How to diff one file to an arbitrary version in Git?
Asked Answered
C

14

428

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

Casilde answered 7/4, 2011 at 19:18 Comment(0)
S
455

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
Serviceberry answered 7/4, 2011 at 19:23 Comment(5)
Note that this doesn't just work for files, it also works for (sub)directories as well, for example git diff <revision>:foo/ HEAD:foo/.Vaudevillian
Also note that on windows you need to use forward slashes for directories if you are using a revision specifier or git will give you an error about the file/directory not existing in that revision.Tedda
@DylanNissley or it will give you no error and no diff. That's what I see. In any case, thank you for the hint about using forward slashes instead of backslashes.Jim
With windows files I've found it easier to change directory and then call git diff master~20:./pom.xml ./pom.xmlWanderjahr
Some version of git require "--" between the <revision> & <path>Hunley
T
191
git diff <revision> <path>

For example:

git diff b0d14a4 foobar.txt
Thromboplastic answered 7/4, 2011 at 19:23 Comment(3)
does not work for version 1.7.11 if file is not in current directory. Example: 'git diff f76d078 test/Config' yields "error: Could not access 'test/f76d078'"Sensual
@user1663987 just pass a full path relative to the project root: git diff <revision> root/path/file.Vaudevillian
test/Config is relative to the root (as in test is a sub-directory of the root). but then your example root/path/file would seem to INCLUDE the root?Sensual
T
58

If you want to see the difference between the last commit of a single file you can do:

git log -p -1 filename

This will give you the diff of the file in git, is not comparing your local file.

Tutor answered 29/8, 2012 at 17:18 Comment(4)
this is not returning anythingSimian
@AndreiCristianProdan Then you have no changes there. You can increment the -1 step by step until you get the changes.Circus
This is very nice. I created a bash function that might be useful: gitlog () { git log -${3:-p} -${2:-1} $1; } Used like: gitlog Rakefile or gitlog Rakefile 5 and gitlog Rakefile 10 s. The first shows one diff; the second shows five diffs; the third shows ten --no-patch.Deviltry
works just fine with WSL Ubuntu 20.04 LTS running on Windows 11.Firebreak
G
52

To see what was changed in a file in the last commit:

git diff HEAD~1 -- path/to/file

You can change the number (~1) to the n-th commit which you want to diff with.

Galluses answered 27/9, 2012 at 23:0 Comment(5)
This answer is really just a specific case of this more general answer, where HEAD~1 is substituted for <revision>, which makes this answer a duplicate.Vaudevillian
this isn't working! fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree. Use '--' to separate paths from revisionsSimian
This answer is simple and functional for me.Semination
"--" separator is missing, I have just fixedTanner
This works better for me! using WSL Ubuntu 20.04 LTS running on Windows 11.Firebreak
W
11
git diff -w HEAD origin/master path/to/file

-w ignores whitespaces

Workingwoman answered 20/10, 2014 at 9:51 Comment(2)
You could at least give some explanation as to what the -w flag means. Writing a solution that works is great, but I personally wouldn't use some random command without knowing what each flag in it means.Heliograph
@TalKohavy You are right, I wrote that in a hurry, I have updated the answer.Workingwoman
B
11

Generic Syntax :

$git diff oldCommit..newCommit -- **FileName.xml > ~/diff.txt

for all files named "FileName.xml" anywhere in your repo.

Notice the space between "--" and "**"

Answer for your question:

$git checkout master
$git diff oldCommit..HEAD -- **pom.xml 
or
$git diff oldCommit..HEAD -- relative/path/to/pom.xml 

as always with git, you can use a tag/sha1/"HEAD^" to id a commit.

Tested with git 1.9.1 on Ubuntu.

Bataan answered 11/3, 2016 at 13:59 Comment(1)
too verbose, almost useless.Firebreak
N
8

If neither commit is your HEAD then bash's brace expansion proves really useful, especially if your filenames are long, the example above:

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

Would become

git diff {master~20,master}:pom.xml

More on Brace expansion with bash.

Nesto answered 20/6, 2017 at 11:31 Comment(0)
Z
7

For comparing to 5 commit to the current one, both on master, just simply do:

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

Also you can refer to commit hash number, for example if the hash number is x110bd64, you can do something like this to see the difference:

git diff x110bd64 pom.xml
Zebedee answered 1/7, 2017 at 12:54 Comment(0)
B
3
git diff master~20 -- pom.xml

Works if you are not in master branch too.

Baro answered 24/3, 2017 at 13:8 Comment(0)
A
2

If you are fine using a graphical tool (or even prefer it) you can:

gitk pom.xml

In gitk you can then click any commit (to "select" it) and right click any other commit to select "Diff this -> selected" or "Diff selected -> this" in the popup menu, depending on what order you prefer.

Autolycus answered 17/7, 2018 at 8:57 Comment(0)
S
1

For people interested in doing the same from GitHub, see comparing commits across time.

Sparge answered 11/8, 2015 at 11:9 Comment(0)
A
1

If you need to diff on a single file in a stash for example you can do

git diff stash@{0} -- path/to/file
Applegate answered 29/2, 2016 at 23:16 Comment(0)
I
1

lets say you want to diff the file interrupt.c located in firmware/src/


difftool between working area (what yet to be committed or staged) and latest commit:

git difftool head firmware/src/interrupt.c

or

git difftool head *interrupt.c

Note: to diff x versions before the last committed version replace "head" with head~x. For example replace "head" with "head~2" to diff between working area and 2 versions prior to the latest commit


difftool between two specific committed versions:

first get the ids for the versions you want to compare by using next line

git log --oneline

you will get a list of all your committed version. choose two and copy their id's.

enter image description here

enter the ids inside {}:

git difftool {2fae9e6,a21dd00} firmware/src/interrupt.c

or

git difftool {2fae9e6,a21dd00} *interrupt.c

Note: if there is no difference between the files, nothing will open. You will just get an empty line in the git bash

Immemorial answered 12/6, 2022 at 7:46 Comment(0)
A
0

If you are looking for the diff on a specific commit and you want to use the github UI instead of the command line (say you want to link it to other folks), you can do:

https://github.com/<org>/<repo>/commit/<commit-sha>/<path-to-file>

For example:

https://github.com/grails/grails-core/commit/02942c5b4d832b856fbc04c186f1d02416895a7e/grails-test-suite-uber/build.gradle

Note the Previous and Next links at the top right that allow you to navigate through all the files in the commit.

This only works for a specific commit though, not for comparing between any two arbitrary versions.

Anglophobe answered 8/2, 2017 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.