2502

I have two branches, branch_1 and branch_2.

How can I see the differences between the two branches in Git?

iliketocode
  • 6,652
  • 4
  • 41
  • 57
isuruanu
  • 25,085
  • 3
  • 12
  • 4
  • 29
    You want something different from the straightforward `git diff branch_1 branch_2`? (Note, if the names branch_1 and branch_2 also name files, you need `git diff branch_1 branch_2 --`) – torek Mar 23 '12 at 05:53
  • 7
    http://stackoverflow.com/questions/822811/differences-in-git-branches – Bijendra Mar 23 '12 at 06:15
  • 62
    The cited duplicate does not answer the question... Determining which files have changed with `git diff --name-status master..branchName` is markedly different than seeing the exact differences between branches with something like `git diff branch_1 branch_2`. Or maybe I'm missing something obvious... – jww Aug 19 '16 at 08:14
  • 48
    Not only is the "duplicate" a different question, this question is the number one google match for "git diff two branches". – Rob Osborne Jun 06 '17 at 13:08
  • 4
    `git difftool branch..otherBranch` lets you SEE the differences in the Visual tool that you choose. e.g. Meld. This is the answer. – Greg Rundlett Jan 30 '18 at 02:18
  • `git diff other_branch -- file_name` - To compare file_name on current branch, with other branch (e.g. master) – Noam Manos Jun 30 '19 at 09:50
  • To restrict the diff to one `file` with the `..` syntax: `git diff branch1..branch2 file` or `git difftool branch1..branch2 file`. – Giuseppe Mar 23 '20 at 13:06
  • in case you want to see the diff on IDEA https://stackoverflow.com/questions/9825106/intellij-viewing-diff-of-all-changed-files-between-local-and-a-git-commit-branc/55159284 – Rajat Aug 18 '20 at 14:02

1 Answers1

3416
git diff branch_1..branch_2

That will produce the diff between the tips of the two branches. If you'd prefer to find the diff from their common ancestor to test, you can use three dots instead of two:

git diff branch_1...branch_2
Lazy Badger
  • 87,730
  • 7
  • 72
  • 97
  • 54
    The same syntax works for comparing a branch with a tag or a tag with another tag. – Daniel Zohar May 27 '13 at 07:02
  • 9
    The range syntax also works for `git log`. With that you can see the commit messages, and with the `--patch` option you can see a diff one commit at a time. – Joe Flynn Feb 10 '14 at 20:20
  • 62
    Note that you can also add a file or folder name after the above two commands. – msanford Jul 28 '14 at 16:54
  • So if the tripple-dot diff finds no common-ancestor differences, then that means `branch_1` has already incorporated all the changes in `branch_2` and a `git merge branch_2` (with `branch_1` checked out) will not affect `branch_1` at all. – hobs Sep 11 '14 at 16:46
  • @DanielZohar Yes, it should work with anything that is a valid commit reference! The git-scm book calls them 'tree-ish' or 'coimmit-ish'. Things such as `tags`, `branches`, `sha1`s, abbreviated `sha1`s, magic things like `HEAD`. (*Though you probably know that by now, since you posted that a year ago.*) – ThorSummoner Oct 02 '14 at 16:30
  • 3
    This doesn't work for me with remote branches. It never returns anything. – capybaralet Oct 27 '14 at 21:22
  • 13
    @chiyachaiya your explanation helped me but git diff b1...b2 is not same as git diff b2...b1. For example once we started b2 from b1 and when if we make some changes to b1, git diff b2...b1 will show changes made to b1 after b2 started. If we do git diff b1...b2 it will give changes made to b2 which are not in b1. – Chintak Chhapia Feb 10 '15 at 06:45
  • 3
    Something I find useful as well is to print the differences out to a file using `git diff branch_1..branch_2 > C:\diff.txt` – vandsh Apr 07 '15 at 18:04
  • And if you use one you just get an error. – Michael J. Calkins May 15 '15 at 20:47
  • chharvey: the order of args matters and will be reflected with different color in the results (green vs red) since code that was added/deleted depends on the order of args. – Vlad Jun 03 '15 at 00:52
  • 2
    Word for word copy of https://github.com/schacon/gitbook/blob/86d1d3061f32e07b56851833c6673e66a04fdbba/text/10_Comparing_Commits_Git_Diff/0_%20Comparing_Commits_Git_Diff.markdown http://schacon.github.io/gitbook/3_comparing_commits_-_git_diff.html – random Jun 30 '15 at 19:43
  • 48
    If you get `fatal: bad revision 'some-branch'` then this is probably a remote branch. You probably need something like `git diff remotes/origin/some-branch my-local-branch` – Dalin Oct 13 '15 at 17:41
  • is their a nice UI for this ie inside webstorm? – SuperUberDuper Oct 15 '15 at 14:08
  • 7
    And why does it only work from the master branch?! (git 2.1.4) I have two repos, cloned from the same one. One has master checked out and the other one has... 'other'. From the 'other' branch: $ git diff other..master __fatal: ambiguous argument 'other..master': unknown revision or path not in the working tree.__ – Sz. Jan 23 '16 at 17:42
  • 1
    Clearly something has changed since this answer because $ git diff b1 b2 fatal: ambiguous argument 'b2': unknown revision or path not in the working tree. $ git diff b1 b2 fatal: bad revision 'web-portal-hosts' $ git diff b1..b2 fatal: ambiguous argument 'b1..b2': unknown revision or path not in the working tree. $ git diff b1..b2 -- fatal: bad revision 'ui-functional-tests..web-portal-hosts' – Keith Tyler Mar 25 '16 at 16:40
  • 2
    And if you wanna see the diff of a specific file between two branches: `git diff branch_1..branch_2 -- filename` – Saad Rehman Shah Jul 29 '16 at 04:28
  • this answer didn't help me. i did this instead: `git checkout local-branch` then `git log` to see which commit you want to see, then, `git diff master ` this is how u can see exactly what changes you made on your branch on each commit compared to master. (no one else uses my local branch except me.) – Awesome_girl Nov 01 '16 at 21:25
  • 1
    Two dots, three dots and two dashes all produce *"unknown revision or path not in the working tree"*. What is so broken with this tool? – jww Jan 07 '17 at 17:46
  • 1
    git difftool -y b1..b2 (works with your configured difftool - in my case p4merge) – danday74 Jan 12 '17 at 14:00
  • 7
    Doesn't work: `fatal: ambiguous argument 'branch1...branch2': unknown revision or path not in the working tree.` – Tomáš Zato - Reinstate Monica Jan 18 '17 at 15:08
  • 1
    If the two branches are very different, the order in which the branches are mentioned does not matter. However, if you know one branch is older than another branch then it will be easier to comprehend the output if you use `git diff ..`. – user3613932 Feb 07 '17 at 03:27
  • 69
    `git diff ..branch_2` compares the checked out branch to branch_2 – sweisgerber.dev Mar 06 '17 at 13:10
  • 2
    In [the original text from which the answer was lifted](https://github.com/schacon/gitbook/blob/86d1d3061f32e07b56851833c6673e66a04fdbba/text/10_Comparing_Commits_Git_Diff/0_%20Comparing_Commits_Git_Diff.markdown) `branch_2` is named `test`. As written here, the second sentence makes no sense. – Ed Randall Jan 20 '18 at 07:15
  • @SaadRehmanShah, I didn't need the '--filename' to view changes of a specific file. git diff .. ^^ This sufficed. Hope this helps. – Supriya Rajgopal Mar 28 '18 at 06:55
  • 1
    If you have the `fatal: ambiguous argument` error, you just need to follow [Dalin](https://stackoverflow.com/users/231914/dalin) 's comment and specify `remotes/origin` (or whatever your remote repo is called). – Zaccharie Ramzi Apr 26 '18 at 13:03
  • 2
    Can also specify path: `git diff BranchName1..BranchName2 --name-only -- /path/to/source/` . `--name-only --` used only for file name – Kulamani Jan 31 '20 at 06:49
  • if you need to compare 2 commits in github - [check this](https://stackoverflow.com/a/49838096/820410) – Pankaj Singhal Oct 06 '20 at 16:51
  • How do you then sift through the horrific UI of the `vi` text editor for each file? – Kyle Vassella Feb 09 '21 at 00:07
  • In case you need to compare your current branch with other branch, you can do: ```git diff ...another_branch``` – John Erbynn Apr 09 '21 at 12:01