Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to diff one file to an arbitrary version in Git?

Tags:

git

git-diff

diff

How can I diff a file, say pom.xml, from the master branch to an arbitrary older version in Git?

like image 238
Chris K Avatar asked Apr 07 '11 19:04

Chris K


People also ask

Can you git diff a specific file?

The git diff command returns a list of all the changes in all the files between our last commit and our current repository. If you want to retrieve the changes made to a specific file in a repository, you can specify that file as a third parameter.

How do I compare files in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch.

Can I use git diff to compare two files?

Comparing files between two different commitsgit diff can be passed Git refs to commits to diff. Some example refs are, HEAD , tags, and branch names. Every commit in Git has a commit ID which you can get when you execute GIT LOG . You can also pass this commit ID to git diff .

What does ++ mean in git diff?

When viewing a combined diff, if the two files you're comparing have a line that's different from what they were merged into, you will see the ++ to represent: one line that was added does not appear in either file1 or file2.


2 Answers

You can do:

git diff master~20:pom.xml pom.xml 

... to compare your current pom.xml to the one from master 20 revisions ago through the first parent. You can replace master~20, of course, with the object name (SHA1sum) of a commit or any of the many other ways of specifying a revision.

Note that this is actually comparing the old pom.xml to the version in your working tree, not the version committed in master. If you want that, then you can do the following instead:

git diff master~20:pom.xml master:pom.xml 
like image 124
Mark Longair Avatar answered Oct 12 '22 09:10

Mark Longair


git diff <revision> <path> 

For example:

git diff b0d14a4 foobar.txt 
like image 43
Benjamin Pollack Avatar answered Oct 12 '22 10:10

Benjamin Pollack