tags:

views:

77

answers:

3

Hey all,

I'm just starting out with Git and I'm looking for a way to get the number of revisions a file has undergone.

Right now I'm using the command:

git diff master~(arbitrary number) main.js

How do I know how high I can go with this number? I would also like to obtain the dates for each revision as well.. Thanks!

A: 

In git, history is normally non-linear, that's why SHA-1 is used to represent a commit instead of revision number.

I suggest you use

git log -p filename

or

git blame filename

to view the history of the file, if that is what you want.

If you insist, this will do it:

git log --pretty=oneline `git log --reverse --pretty=%H filename | head -1`.. | wc -l
Iamamac
+1  A: 

git-log is your friend here.

git log --pretty=oneline main.js | wc -l

git log also has some diff and patch related options. Do try

git log -p main.js

Edit: As Iamamac pointed out in the comment below, the above gives you the number of edits done to the file. What you truly want is the number of commits since the first check in of the file to the master. How about

git log master --oneline `git log master --reverse --pretty=%H main.js | head -1`..master | wc -l

This should work well in any branch. Thanks for asking this question. I have added a git-master-diff to my bin folder that contains

#!/usr/bin/env bash
git diff master~$(git log master --oneline `git log master --reverse --pretty=%H $@ | head -1`..master | wc -l )  $@

Should come in handy.

anshul
The number you get is how many times `main.js` is changed, not the "distance" between the first and latest commit, which is what Matt exactly wants to get
Iamamac
Thanks for pointing this out.
anshul
+1  A: 

Try this

git log --pretty="format:%ai | %s" main.js

for the revisions along with dates. You can pipe this through wc -l for the number of revisions. For printing different bits of information, try

git help log

and check out the format: parameter to the --pretty option.

Noufal Ibrahim
Awesome thanks so much!
Matt