blueFast
blueFast

Reputation: 44381

See Nth previous version of file

I would like to see an older version of a file, but this:

git show HEAD~1:main.c

gives me the previous commit. That file has not changed since the previous commit, so I see the same content. In order to find an older version of that file, I need to start increasing manually the revision spec until I find an actual difference. This is frustrating.

In this particular case, I do not care about commits: I just want to see previous versions of this specific file. How can I give a revision specification of the type "the Nth oldest version"? The revision specifications I know are always commit-related, not file-version related.

The closest I have got is:

git log --follow --pretty=oneline -- annotated-bower.json

Which gives me a list of the commits where this file has been changed. Now that I see this list, how can I tell git show something like:

git show OLDER~3:main.c

Without having to manually copy-paste the commit spec:

git show b27a57c6732200d8ef8b5b8c87d07fe67f37e9db:main.c

I can of course implement this myself:

git-previous.sh main.c 3

Parsing the output of git log --follow --pretty=oneline to get the commit hash and call then git show with that, but I would like to avoid reinventing the wheel. Besides, this seems like a so useful feature, that this is surely part of stock git, and I have just overseen it, right?

Upvotes: 3

Views: 173

Answers (1)

eckes
eckes

Reputation: 67087

Well you don't have to parse anything if you feed git log correctly:

git log -10 --follow --pretty=%h -- /path/to/your/file

prints out the last 10 (-10) short commit hashes (%h) for your file.

Take the output of git log, put it into a list and loop over the entries. Done.

Perhaps this is the reason why there's no separate command to achieve this... And regarding your note about

Besides, this seems like a so useful feature [...]

I'm using git since a good while now and never actually had the need for viewing the history of a file in a sequence. Changes are interesting. Therefore, you could also feed a path to gitk which will result in a list of changes displayed with their differences in gitk:

gitk -- /path/to/your/file

Upvotes: 3

Related Questions