How do I call git diff on the same file between 2 different local branches?

34,434

Solution 1

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.

Solution 2

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

git diff develop:foo.txt master:bar.txt

Solution 3

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

Share:
34,434
akantoword
Author by

akantoword

javascript-ninja in training

Updated on July 13, 2020

Comments

  • akantoword
    akantoword almost 4 years

    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.

  • PRS
    PRS over 6 years
    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
  • Robert Calhoun
    Robert Calhoun over 3 years
    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.