40

Is there a way to check the difference between the working directory in my current branch against the working directory of another branch in a specific file? For example, if I'm working in branch A on file 1, I want to compare the difference with file 1 on branch B.

akantoword
  • 2,824
  • 8
  • 26
  • 43
  • 2
    Possible duplicate of [How to compare files from two different branches?](https://stackoverflow.com/questions/4099742/how-to-compare-files-from-two-different-branches) – faintsignal Feb 26 '19 at 16:27

3 Answers3

71

Use the git diff branch branch filename syntax:

git diff branchA branchB -- file.cs

put the two names of your branches and optionally use the -- filename option to specify one file! Good luck, let me know if you have issues.

Patrick Murphy
  • 2,311
  • 14
  • 17
  • 6
    I don't think you need the `--` portion, and you can diff more than one file or directory this way. If you don't pass anything, it shows the entire branch diff. `git diff branchA branchB` - diff between branches on entire repo `git diff branchA branchB file.cs` - diff on that 1 file `git diff branchA branchB some/path/in/rep some/other/path` - diffs two directories – PRS Oct 06 '17 at 06:29
21

The following will help if you want to compare different files in two branches

git diff develop:foo.txt master:bar.txt
arulraj.net
  • 4,579
  • 3
  • 36
  • 37
  • This was helpful: I needed to compare files that had moved around in the linux source. `git diff linux-v4.1.10:sound/soc/davinci/davinci-mcasp.c linux-5.4.y:sound/soc/ti/davinci-mcasp.c` did the trick. – Robert Calhoun Oct 02 '20 at 03:09
10

Say you're on one branch, called legacy, and want to compare the file build.gradle to the same version on the master branch:

git diff master build.gradle

Don Branson
  • 13,631
  • 10
  • 59
  • 101