2761

I have two branches: branch_1 and branch_2.

How can I see the differences between them?

Mus
  • 6,790
  • 22
  • 78
  • 117
isuruanu
  • 27,675
  • 3
  • 13
  • 4
  • 33
    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
  • 67
    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
  • 52
    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

6 Answers6

3642

You want to use git diff.

git diff [<options>] <commit>..​<commit> [--] [<path>…​]

Where <commit> is your branch name, the hash of a commit or a shorthand symbolic reference

For instance git diff abc123..def567 or git diff HEAD..origin/master

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 <commit>...<commit>

And if you just want to check which files differ, not how the content differs, use --name-only:

git diff --name-only <commit>..​<commit>

Note that in the <commit>..<commit> (two dot) syntax, the dots are optional; the following is synonymous:

git diff commit1 commit2
sjw
  • 100
  • 5
Lazy Badger
  • 91,325
  • 7
  • 76
  • 105
  • 57
    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
  • 67
    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
  • 50
    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
  • 3
    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
  • 9
    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
  • 80
    `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
  • 1
    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
  • Your first and second lines are identical. It appears to me that you wanted to omit the dots in the first line...? – Johannes Schaub - litb Sep 12 '21 at 09:06
69

It's very simple. You just go to one branch (e.g. main is your branch).

Run the command

git checkout main
git diff branch2
Neeraj Kumar
  • 4,573
  • 27
  • 21
  • this didn't work for me, it showed no changes. I only had local commits though. – rob Mar 04 '22 at 11:46
14

Code is simply git diff master..develop

Options:

  • You can add --name-only to only see the names of the files.
  • If you want to see the changes of specific files or folders. Then add -- folderOrFileName at the end.
  • If you want to compare the local branch with the remote one, then fetch --all to fetch all remote branches, and run git diff --name-only [branchName]..origin/[branchName]. For example git diff --name-only develop..origin/develop
Nagibaba
  • 2,919
  • 1
  • 27
  • 34
8

There are many different ways to compare branches, and it's depend on the specific use case you need.

Many times you want to compare because something broken and you want to see what has been changes, then fix it, and see again what changed before commiting.

Personally when I want to see the diff what I like to do:

git checkout branch_1 # checkout the oldest branch
git checkout -b compare-branch # create a new branch
git merge --no-commit --squash branch_2 # put files from the new branch in the working folder
git status # see file names that changes
git diff # see the content that changed.

Using this solution you will see the diff, you can also see only the file names using git status, and the most important part you will be able to execute branch_2 while seeing the diff (branch_2 is on the working tree). If something had broken you can editing the files and fix it. Anytime, you can type again git status or git diff to see the diff from the new edit to branch_a.

Aminadav Glickshtein
  • 20,647
  • 11
  • 72
  • 114
7

You can simply show difference by- git diff b1...b2 Or you can show commit difference using- git log b1..b2 You can see commit difference in a nice graphical way using - git log --oneline --graph --decorate --abbrev-commit b1..b2

Bip Lob
  • 127
  • 4
  • 13
0

In Eclipse(J2EE version) , open "Window --> Show view --> Git Repository". if you have checked out 2 local git branches for examples then you will have bunch of branches in Local section. select any 2 git local branches and do " right click and select "Compare with each other in Tree menu".

Open view "Git Tree Compare" and u will be able to see side by side diff for all files.

Anuj
  • 1
  • 1