[git] How to compare files from two different branches?

I have a script that works fine in one branch and is broken in another. I want to look at the two versions side-by-side and see what's different. Are there any ways to do this?

To be clear I'm not looking for a compare tool (I use Beyond Compare). I'm looking for a git diff command that will allow me to compare the master version to my current branch version to see what has changed. I'm not in the middle of a merge or anything. I just want to say something like

git diff mybranch/myfile.cs master/myfile.cs

This question is related to git git-diff

The answer is


There are two scenarios to compare files:

Scenario 1: Compare files at remote branches (both branches should exists on remote repository)

Scenario 2: Compare local files (at local working area copy) to the files at remote repository.

The logic is simple. If you provide two branch names to diff, it will always compare the remote branches, and if you provide only one branch name, it will always compare your local working copy with the remote repo (the one you provided). You can use range to provide remote repositories.

e.g. Checkout a branch

git checkout branch1
git diff branch2 [filename]

in this case, if you provide filename, it will compare your local copy of filename with remote branch named "branch2".

git diff branch1 branch2 [filename]

in this case, it will compare filename from remote branches named "branch1" vs "branch2"

git diff ..branch2 [filename]

in this case also, it will compare filename from remote branches named "branch1" vs "branch2". So, its same as above. However, if you have just created a branch from another branch, say "master" and your current branch doesn't exists on remote repository, it will compare remote "master" vs remote "branch2".

Hope its useful.


There are many ways to compare files from two different branches:

  • Option 1: If you want to compare the file from n specific branch to another specific branch:

    git diff branch1name branch2name path/to/file
    

    Example:

    git diff mybranch/myfile.cs mysecondbranch/myfile.cs
    

    In this example you are comparing the file in “mybranch” branch to the file in the “mysecondbranch” branch.

  • Option 2: Simple way:

     git diff branch1:file branch2:file
    

    Example:

     git diff mybranch:myfile.cs mysecondbranch:myfile.cs
    

    This example is similar to the option 1.

  • Option 3: If you want to compare your current working directory to some branch:

    git diff ..someBranch path/to/file
    

    Example:

    git diff ..master myfile.cs
    

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


The best way to do it is by using git diff in the following way: git diff <source_branch> <target_branch> -- file_path

It will check the difference between files in those branches. Take a look at this article for more information about git commands and how they work.


You can do this: git diff branch1:path/to/file branch2:path/to/file

If you have difftool configured, then you can also: git difftool branch1:path/to/file branch2:path/to/file

Related question: How do I view git diff output with visual diff program


If you want to make a diff against current branch you can ommit it and use:

git diff $BRANCH -- path/to/file

this way it will diff from current branch to the referenced branch ($BRANCH).


In my case, I use below command:

git diff <branch name> -- <path + file name>

This command can help you compare same file in two different branches


In order to compare two files in the git bash you need to use the command:

git diff <Branch name>..master -- Filename.extension   

This command will show the difference between the two files in the bash itself.


Use commit hashes as this:

git diff <hash1> <hash2> <filename>

where hash1 can be any commit from any branch, the same for hash2.


Agreeing with the answer suggested by @dahlbyk. If you want the diff to be written to a diff file for code reviews use the following command.

git diff branch master -- filepath/filename.extension > filename.diff --cached

More modern syntax:

git diff ..master path/to/file

The double-dot prefix means "from the current working directory to". You can also say:

  • master.., i.e. the reverse of above. This is the same as master.
  • mybranch..master, explicitly referencing a state other than the current working tree.
  • v2.0.1..master, i.e. referencing a tag.
  • [refspec]..[refspec], basically anything identifiable as a code state to git.

I simply do git diff branch1 branch2 path/to/file

This checks for differences between the files. Changes in branch1 would be in red. Changes in branch2 would be in green.

It's assumed that branch1 is the past and branch2 is the future. You can reverse this by reversing the order of the branches in the diff: git diff branch2 branch1