Git
does not store any numeric version of the objects from the repository. I think you want to know how many times the object was changed (stored in the repository) and this is easy to get: use git log
, tell it to produce a single line of text for each commit and count the lines of its output:
To count the versions of file foo
stored in the repository that are accessible from the current branch run this:
git log --oneline HEAD -- foo | wc -l
You can replace HEAD
with a branch name to get the number of commits reachable from that branch that store changes on file foo
or you can remove it altogether.
Keep in mind that the number you get is not unique for each version of the file and it depends on the path you use to get the log.
Check the next fragment of revision graph:
* commit #4 (merge test into master)
|\
* | commit #3 (on branch master)
| * commit #2 (on branch test)
|/
* commit #1 (on master)
Let's say the file foo
was added on commit #1 and modified on all the other commits displayed.
Running the command above on different commits will produce the following values:
+--------+---------------+
| commit | # of versions |
+--------+---------------+
| #1 | 1 |
| #2 | 2 |
| #3 | 2 |
| #4 | 4 |
+--------+---------------+