MaiaVictor
MaiaVictor

Reputation: 53067

How to get an older version of a file using git?

Is there any way, using the git API, to programmatically get a list of versions of a specific file, and then get the contents of a specific one?

Upvotes: 0

Views: 112

Answers (2)

albert
albert

Reputation: 4468

To get a list of versions of a specific file

git log --relative <<file_path>>

To get the contents of the file

git show <<commit_hash>>:<<file_path>>

Upvotes: 0

David Cain
David Cain

Reputation: 17353

Sure. You can start with rev-list to get a reverse chronological (i.e. newest first) list of hashes where the file in question was modified:

git rev-list HEAD <filename>

You can then use git show <hash>:<filepath> to show a file's contents.

Show contents at every hash

I'm not sure if you want to see every version's contents, but you could loop through each with a simple for loop:

for hash in `git rev-list HEAD dummy_repo/file.c`
do
    git show $hash:dummy_repo/file.c
done

Commit selection

For more feedback on what each commit changes, try git whatchanged <filename> (you'll be able to see the hash, type of modification, as well as the commit message).

Try git whatchanged -p <filename> to see what changes the commit actually introduced in the file. When you have a hash you like, you can again use git show <hash>:<filepath> for the complete contents of the file.

Upvotes: 3

Related Questions