How can I diff a file, say pom.xml
, from the master branch to an arbitrary older version in Git?
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.
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.
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 .
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.
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
git diff <revision> <path>
For example:
git diff b0d14a4 foobar.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With