I want to merge two branches that have been separated for a while and wanted to know which files have been modified.

Came across this link: http://linux.yyz.us/git-howto.html which was quite useful.

The tools to compare branches I've come across are:

  • git diff master..branch
  • git log master..branch
  • git shortlog master..branch

Was wondering if there's something like "git status master..branch" to only see those files that are different between the two branches.

Without creating a new tool, I think this is the closest you can get to do that now (which of course will show repeats if a file was modified more than once):

  • git diff master..branch | grep "^diff"

Was wondering if there's something I missed...

  • 6
    How many others find the title of this question misleading? It is actually about finding the file differences between two branches. What I came here looking for was how to see file differences between two revisions on the same branch. Or am I the only one? – Sandeepan Nath Mar 18 '16 at 10:19
  • 1
    @SandeepanNath: with git there is no difference. You are ALWAYS referring to individual commits. – Samuel O'Malley Mar 21 '16 at 2:54
  • @SamuelO'Malley I am new to git and considering the seemingly common branching strategy wherein all the branches are finally merged to the master branch and ultimately the master is rolled out. Now, considering the event of a rollout, where the production is already at master, but behind the tip (by one revision if the last rollout happened after the last master merge), I would like to see the differences between these two revisions, to find out what would be rolled out. I would not like to look at the branch which was last merged. Correct me if I am wrong. – Sandeepan Nath Mar 21 '16 at 6:22
  • 1
    @SandeepanNath: instead of using the branch names then you can take the answers below and just specify the commit IDs instead. Or even refer the commits by their tag names if you create tags when rolling out. – Samuel O'Malley Mar 21 '16 at 6:50
  • 1
    @SandeepanNath You cannot compare 2 branches, you must specify the revision. So comparing 2 branches is comparing 2 revisions. – Bastien Vandamme Dec 15 '17 at 15:37

14 Answers 14

up vote 2193 down vote accepted

To compare the current branch against master

$ git diff --name-status master

To compare any pair of branches

$ git diff --name-status firstbranch..yourBranchName

That should do what you need, if I understand you correctly.

  • 209
    +1, sometimes --name-only is handy too – Pat Notz May 5 '09 at 4:16
  • 12
    @user446936 - you can see what the letters mean in the git status man page @ kernel.org/pub/software/scm/git/docs/git-status.html - in particular, M == modified, D == deleted – James Manning Apr 11 '13 at 18:34
  • 6
    git diff master..blabla -- gives me fatal: bad revision 'master..blabla'. git diff master blabla -- gives me fatal: bad revision 'master'. Any idea what's wrong? UPDATE: this repo does not have a branch named master, doh! – akostadinov Jul 30 '13 at 10:48
  • 7
    git diff --name-status your_branch...master outputs the changes that occurred on master since your_branch was created from it – Radu Aug 20 '13 at 10:38
  • 4
    Here is a link to more options when using git diff. git-scm.com/docs/git-diff – akvallejos Sep 17 '14 at 15:36

Try

$ git diff --stat --color master..branchName

This will give you more info about each change, while still using the same number of lines.

You can also flip the branches to get an even clearer picture of the difference if you were to merge the other way:

$ git diff --stat --color branchName..master
  • 69
    If you have (highly recommended, imho) git color turned on (config --global color.ui true), you can skip the --color. (I have lks - lazy keyboard syndrome.) – Art Swri Mar 10 '12 at 21:35
  • 22
    I'm with you on color! BTW I meant to say git config --global color.ui true - to be complete. – Art Swri Mar 10 '12 at 22:13
  • 1
    Does not work, throws errors: fatal: ambiguous argument 'master..branchName': unknown revision or path not in the working tree. – Tomáš Zato Jan 18 '17 at 15:15
  • 6
    @TomášZato sorry but you need to swap "branchName" with the name of your branch. – Gerry Jan 30 '17 at 12:01

Also keep in mind that git has cheap and easy branching. If I think a merge could be problematic I create a branch for the merge. So if master has the changes I want to merge in and ba is my branch that needs the code from master I might do the following:

git checkout ba
git checkout -b ba-merge
git merge master
.... review new code and fix conflicts....
git commit
git checkout ba
git merge ba-merge
git branch -d ba-merge
git merge master

End result is that I got to try out the merge on a throw-away branch before screwing with my branch. If I get my self tangled up I can just delete the ba-merge branch and start over.

  • 4
    Awesome. I have never thought of branching that way. I think this should be considered as part of the "best practices" when merging. – egelev Jul 1 '15 at 14:08
  • When you merge the ba-marge back into ba, isn't there a potential to have to fix the conflicts again? – Josef.B May 2 '17 at 2:48
  • 1
    Nope, you've already resolved them. – Eric Anderson May 2 '17 at 2:48
  • 2
    @EricAnderson Right, it's a graph. SVN sticks like gum under a school desk. thx. – Josef.B May 2 '17 at 4:10

If anyone is trying to generate a diff file from two branches :

git diff master..otherbranch > myDiffFile.diff
  • 2
    This has come in handy especially with large branches containing a lot of differences. – vandsh Apr 7 '15 at 18:07

Note that git makes it easy to just try out the merge and back away from any problems if you don't like the result. It might be easier than looking for potential problems in advance.

  • 10
    David, that's a good point, although it'd be nice to just know what's going on before hand... – johannix May 5 '09 at 1:14

There is also a GUI based method.

You can use gitk.

  1. Run:

    $ gitk --all
    
  2. Right click on a commit of a branch and select Mark this commit in the pop-up menu.

  3. Right click on a commit of another branch and select Diff this -> marked commit or Diff marked commit -> this.

Then there will be a changed files list in the right bottom panel and diff details in the left bottom panel.

  • 3
    @Orwellophile I upload a video to show how to do it. I hope it will help to you. – Shawn Xie Oct 20 '15 at 9:43
  • Wow, just for me, I feel special. I've bookmarked it in delicious.com for future reference and extra google-foo. – Orwellophile Oct 21 '15 at 15:29

One more option, using meld in this case:

git difftool -d master otherbranch

This allows not only to see the differences between files, but also provides a easy way to point and click into a specific file.

  • 6
    May want to set meld as default difftool: git config --global diff.tool meld – bwv549 Sep 8 '14 at 15:19
  • 1
    This is my favorite because it will use whatever difftool you configure. – Josiah Mar 3 '17 at 15:08
  • Not supported on OSX. :-( – Mike S. Jun 10 at 12:36
  • @MikeS. please checkout this answer stackoverflow.com/a/12815806/151918 it contains instructions for OSX. It works for me at least, hope it helps. – rsilva4 Jun 11 at 10:39

When working collaboratively, or on multiple features at once, it's common that the upstream or even your master contains work that is not included in your branch, and will incorrectly appear in basic diffs.

If your Upstream may have moved, you should do this:

git fetch
git diff origin/master...

Just using git diff master can include, or fail to include, relevant changes.

And if you are looking for changes only among certain file(s), then:

git diff branch1 branch2 -- myfile1.js myfile2.js

branch1 is optional and your current branch (the branch you are on) will be considered by default if branch1 is not provided. e.g:

git diff master -- controller/index.js

If you are using IntelliJ IDEA, you can also compare any branch with your current working branch. See http://www.jetbrains.com/idea/webhelp/merging-deleting-and-comparing-branches.html#d288093e3827 for more info. This is available in the free edition as well.

There are two branches lets say

  • A (Branch on which you are working)
  • B (Another branch with which you want to compare)

Being in branch A you can type

git diff --color B

then this will give you a output of

enter image description here

The important point about this is

  1. Text in green is inside present in Branch A

  2. Text in red is present in Branch B

There are plenty of answers here, but I wanted to add something that I commonly use. IF you are in one of the branches that you would like to compare I typically do one of the following. For the sake of this answer we will say that we are in our secondary branch. Depending on what view you need at the time will depend on which you choose, but most of the time I'm using the second option of the two. The first option may be handy if you are trying to revert back to an original copy -- either way, both get the job done!

This will compare master to the branch that we are in (which is secondary) and the original code will be the added lines and the new code will be considered the removed lines

git diff ..master

OR

This will also compare master to the branch that we are in (which is secondary) and the original code will be the old lines and the new code will be the new lines

git diff master..

If you like GUI and are using Windows, here is an easy way.

  1. Download WinMerge
  2. Check out the two branches into different folders
  3. Do a folder by folder compare using WinMerge. You can also easily make modifications if one of the branches is the one you are working on.

You can also easily compare branches for changed files using for example TortoiseGit. Just click on Browse References and pick the branches you want to compare.

For example if you compare your branch with master you will get as a result list of files that will be changed in master if you decide to merge your-branch into master.

Remmber that you will have different result if you compare master with your-branch and your-branch with master.

  • the question seems to be about native git utility – Vladimir Hraban Jan 30 at 14:52

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.