How to compare files from two different branches How to compare files from two different branches git git

How to compare files from two different branches


git diff can show you the difference between two commits:

git diff mybranch master -- myfile.cs

Or, equivalently:

git diff mybranch..master -- myfile.cs

Note you must specify the relative path to the file. So if the file were in the src directory, you'd say src/myfile.cs instead of myfile.cs.

Using the latter syntax, if either side is HEAD it may be omitted (e.g., master.. compares master to HEAD).

You may also be interested in mybranch...master (from git diff documentation):

This form is to view the changes on the branch containing and up to the second <commit>, starting at a common ancestor of both <commit>. git diff A...B is equivalent to git diff $(git-merge-base A B) B.

In other words, this will give a diff of changes in master since it diverged from mybranch (but without new changes since then in mybranch).


In all cases, the -- separator before the file name indicates the end of command line flags. This is optional unless Git will get confused if the argument refers to a commit or a file, but including it is not a bad habit to get into. See Dietrich Epp's answer to Meaning of Git checkout double dashes for a few examples.


The same arguments can be passed to git difftool if you have one configured.


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 my preferred diff tool/ viewer?


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.