Chris K
Chris K

Reputation: 12341

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

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

Upvotes: 438

Views: 358609

Answers (15)

matt91t
matt91t

Reputation: 171

Check diff in all files from specific directory between eg. develop branch and feature/my_feature branch:

git checkout develop
git diff feature/my_feature: src/main/

Upvotes: 0

CatalinBerta
CatalinBerta

Reputation: 1724

git diff -w HEAD origin/master path/to/file

-w ignores whitespaces

Upvotes: 13

Juampy NR
Juampy NR

Reputation: 2818

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.

Upvotes: 53

epic
epic

Reputation: 1443

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

Upvotes: 1

Martin G
Martin G

Reputation: 18109

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.

Upvotes: 2

Alireza
Alireza

Reputation: 104650

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

Upvotes: 7

robstarbuck
robstarbuck

Reputation: 8091

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.

Upvotes: 8

Gunar Gessner
Gunar Gessner

Reputation: 2631

git diff master~20 -- pom.xml

Works if you are not in master branch too.

Upvotes: 3

Schmick
Schmick

Reputation: 310

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.

Upvotes: 0

user5772635
user5772635

Reputation:

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.

Upvotes: 11

lacostenycoder
lacostenycoder

Reputation: 11186

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

git diff stash@{0} -- path/to/file

Upvotes: 1

Skippy le Grand Gourou
Skippy le Grand Gourou

Reputation: 7694

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

Upvotes: 1

Mark Longair
Mark Longair

Reputation: 467033

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

Upvotes: 467

Gerardo
Gerardo

Reputation: 7767

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.

Upvotes: 61

Benjamin Pollack
Benjamin Pollack

Reputation: 28402

git diff <revision> <path>

For example:

git diff b0d14a4 foobar.txt

Upvotes: 200

Related Questions