3020

How do I see the differences between branches branch_1 and branch_2?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
isuruanu
  • 30,273
  • 3
  • 14
  • 4
  • 37
    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
  • 72
    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
  • 54
    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
  • 6
    `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
  • 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

9 Answers9

3823

Use git diff.

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

<commit> is a branch name, a commit hash, or a shorthand symbolic reference.

Examples: git diff abc123..def567, 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>

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
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Lazy Badger
  • 94,711
  • 9
  • 78
  • 110
  • 61
    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
  • 70
    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
  • 52
    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
  • 8
    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
  • 2
    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
  • 10
    Doesn't work: `fatal: ambiguous argument 'branch1...branch2': unknown revision or path not in the working tree.` – Tomáš Zato 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
  • 83
    `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
  • 2
    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
  • @Lazy Badger What does `[--]` mean? – John Jun 09 '22 at 07:28
  • @Lazy Badger What does `[--]` mean? – John Jun 09 '22 at 07:28
  • Can the answer be edited to show an example of diffing two remote branches? I can't figure out how to get past the `fatal: ambiguous argument` even when I specify `remotes/origin` (after confirming my remote repo is called "origin"). – Daryl Spitzer Aug 24 '22 at 21:28
  • @DarylSpitzer - `remotes/origin` **IS NOT** branch, it's repo, as you noted. You **must** use branches, as for local case, just prefixed with `remotes/` each remote branch – Lazy Badger Aug 25 '22 at 09:45
144

Go to a branch (e.g. main), then run diff against another branch (e.g. branch2):

git checkout main
git diff branch2
John Smith
  • 7,243
  • 6
  • 49
  • 61
Neeraj Kumar
  • 6,045
  • 2
  • 31
  • 23
57
git diff master..develop

Options:

  • Add --name-only to only see the names of the files.
  • Add -- folderOrFileName at the end to see the changes of specific files or folders.
  • To compare the local branch with the remote one, then run git fetch --all to fetch all remote branches, and run:
    git diff --name-only [branchName]..origin/[branchName]
    
    Example: git diff --name-only develop..origin/develop.
John Smith
  • 7,243
  • 6
  • 49
  • 61
Nagibaba
  • 4,218
  • 1
  • 35
  • 43
18

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
  • 485
  • 2
  • 6
  • 15
13

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
  • 23,232
  • 12
  • 77
  • 117
7

There are two ways to see the differences between two branches.The modifications that have been made to the files in each branch will be shown by these commands.

  1. Use the git diff command to view the differences between two branches in a Git repository.

    git diff branch1 branch2 will show all the differences.

    If you wish to compare a specific file between the two branches, you can use this command as:

    git diff branch1 branch2 path/to/file

  2. The git log command can also be used to view the differences between two branches. Run the git log command with the —left-right parameter and the two branches you wish to compare like this:

    git log --left-right branch1...branch2

Ritik Banger
  • 2,107
  • 5
  • 25
  • 1
    The second option is exactly what I was looking for. This is the perfect one-liner for a quick diff _by commit_. For an even quicker look. to refer to the current branch's position use `HEAD`, and to narrow to just commit messages add the aptly named `--oneliner` option. For example: `git log --left-right --oneline HEAD...branch2` – Boaz Jun 02 '23 at 14:44
6

Sometimes it's nice to see the diff as a tree...

git difftool --dir-diff branch..otherBranch

or to compare a remote branch to the local workspace...

git difftool --dir-diff origin/branch .

For example, when bitbucket decides for performance reasons it will only show you a "three way merge" diff rather than the actual complete differences between the two branches you've selected.

This will show the diff as a tree in the tool you've selected. e.g. in meld.

Inspired by @GregRundlett comment.

poleguy
  • 523
  • 7
  • 11
  • I had neglected to link to the damning admission that they've removed a key diff capability to optimize their performance ('cost?'). – poleguy Feb 06 '23 at 16:17
1

When on the feature branch, merge your target branch and then run a diff against it. For example, if you want to see what changes your feature branch add to master, do the following:

// Fetch from all remotes
git fetch

// Check out your feature branch
git checkout feature

// Merge origin/master to your branch
git merge origin/master

// Compare to origin/master
git diff origin/master
Liran H
  • 9,143
  • 7
  • 39
  • 52
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