218

I have two different files in different branches. How can I diff them in one command?

Something like

# git diff branch1/foo.txt branch2/foo-another.txt

I could check out the other file, diff it and restore, but that's quite dirty solution.

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • 5
    @EugenKonkov It's not a duplicate because this question is asking how to diff **different files** in different branches. The linked question only asks how to diff the **same file** in different branches. – Steve Jul 07 '18 at 03:58
  • [If one or both files is in the working tree (file system), consider this question](https://stackoverflow.com/questions/16683121/git-diff-between-two-different-files) – Josiah Yoder Dec 13 '22 at 23:13

5 Answers5

277
git diff branch1:full/path/to/foo.txt branch2:full/path/to/foo-another.txt

You can also use relative paths:

git diff branch1:./relative/path/to/foo.txt branch2:./relative/path/to/foo-another.txt
twaggs
  • 3,591
  • 1
  • 14
  • 8
  • 11
    Awesome! I was certainly not able to infer that from `git help diff`. By the way, those don't have to be branch names ahead of the colons, but can be any kind of commit references (e.g. SHA-1 values). – Steve Jorgensen Jun 29 '12 at 19:16
  • 3
    Important Note: Git on windows requires the full itemspec to be a unix name. i.e. branch1:full\path\to\foo.txt fails, while branch1:full/path/to/foo.txt works fine, as does full\path\to\foo.txt (no branch) – Eris Nov 20 '13 at 20:51
  • use `git difftool` and then drop the `branch2:` and that will allow you to edit a file in the current working tree (to bring over changes from `branch1`) – gMale Oct 06 '17 at 02:17
  • 1
    Tried on linux with git version 1.8.3.1, only relative paths allowed. – Sola Yang Nov 15 '17 at 22:20
25

Sidenote: no need for full paths, you can start with ./ for relative paths. It can be handy sometimes.

git diff branch1:./relative/path/to/foo.txt branch2:./relative/path/to/foo-another.txt
Campa
  • 4,267
  • 3
  • 37
  • 42
12

Off-topic answer -- diffing the same file in different branches

Just to add it for I find it a very straightforward syntax :

git diff <branch1> <branch2> <filepath>

Also works with relative refs like for example :

# compare the previous committed state from HEAD with the state branch1 was 3 commits ago
git diff HEAD^ <branch1>~3 <filepath>
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 5
    The OP specifically asked for "different files". Your answer is about comparing the same file in two different branches. – Étienne Miret Oct 16 '19 at 08:35
  • 1
    @EtienneMiret You're absolutely right, I missed that important point. Off-topic answer. – Romain Valeri Oct 16 '19 at 08:51
  • 4
    Still, many people land on this question because they want to get the difference for the same file, and this answer is useful. – Matthieu Mar 30 '22 at 12:15
  • @Matthieu Then why not link to a relevant answer rather? – jtlz2 Oct 25 '22 at 09:48
  • 1
    @jtlz2 I'm not sure I understand. This answer *is* relevant for that problem (albeit not the OP). The cause is Google that lands us here ;) – Matthieu Oct 25 '22 at 13:19
4

There are many ways to compare files from two diferents branchs. For example:

  • If the name is the same or different:

     git diff branch1:file branch2:file
    

    Example:

     git diff branch1:full/path/to/foo.txt branch2:full/path/to/foo-another.txt
    
  • Only if the name is the same and you want to compare your current working directory to some branch:

    git diff ..someBranch path/to/file
    

    Example:

    git diff ..branch2 full/path/to/foo.txt
    

    In this example you are comparing the file from your actual branch to the file in the master branch.

You can check this response:

Compare a file from two different branchs in Git

Javier C.
  • 7,859
  • 5
  • 41
  • 53
  • "Only if the name is the same and you want to compare your current working directory to some branch": This is misleading. It compares **the latest commit to the current branch"" to some other branch. Your wording implies that it will compare the file actually found on disk, if there are uncommitted changes; it's not so. – Hammerite Sep 02 '22 at 10:01
-1

You can specify a start and range for git diff to be applied to. The range is indicated with the .. notation.

branch1=somebranch
branch2=someotherbranch
git diff ${branch1}..${branch2} -- file_path
Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42
JCF
  • 368
  • 3
  • 15
  • 1
    This is only comparing the same file in two branches; the question was about two different files in two branches. – Piran Apr 08 '20 at 16:26
  • @Piran and I landed on this question because I wanted to compare the same file in two branches so I'm happy this answer exists. – Matthieu Mar 30 '22 at 12:16