Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to diff two different files in different branches?

Tags:

git

git-diff

People also ask

How do I compare two files in different branches?

Compare specific file between two branches In some cases, you may want to see all changes done to a specific file on the current branch you are working on. In order to see the differences done to a file between two branches, use the “git diff” command, specify the two branches and the filename.


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

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

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


Off-topic answer -- see comments

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>