216

I am just wondering if there is a way to simply diff two branches in GitHub? I know GitHub has capacity to do it because when we do code-reviews it does list out all the diffs nicely. I was just wondering if there is a way to do it without any code review to compare say Branch to Branch or Commit to Commit? So that when I push something to my remote branch and I want to see how my diffs are going to look like BEFORE it create a PR then it can be very helpful.

I can always be in console and do git diff but that is really not as nice and visually clear as how it shows up in web UI of GitHub. Any ideas?

Lost
  • 12,007
  • 32
  • 121
  • 193

7 Answers7

204

Use the compare feature to accomplish this.

To compare different versions of your repository, append /compare to your repository's path.

user56reinstatemonica8
  • 32,576
  • 21
  • 101
  • 125
Ari M.
  • 2,181
  • 1
  • 13
  • 7
  • 31
    That's very useful. Meanwhile, I can't believe they hide this wonderful feature in a doc, rather than providing a link to it from the project homepage. – RayLuo Jul 05 '17 at 21:02
  • 2
    is there a way to do this through the command like? I tried git compare branch_1 branch_2, but it did not work – Bremsstrahlung Jun 11 '19 at 00:17
  • 6
    @Bremsstrahlung yes: `git diff branch_1 branch_2` – Tim Peters Jun 12 '19 at 06:49
  • 12
    Just add `/compare` to the end of your repo url and you're there! – Brad Parks Aug 28 '19 at 14:08
  • 1
    They seem to have dropped this quite recently. I can't find anything like this right now. – AlanSE Aug 05 '21 at 15:24
  • 4
    I used /compare/ just now and it worked. – Wrench Mar 09 '22 at 10:59
  • 1
    @AlanSE `github.com/your-repo/compare` will let you choose each branch, working as of 05/2022 – Eduardo06sp Jun 01 '22 at 05:51
  • but does it show real difference or just takes commits and displays what's there. I had conflict on feature branch, merged master in, fixed conflict, commit and now gazilion extra changes that are not real changes, just history from master. – Dainius Aug 17 '22 at 07:21
79

If you are on any branch other than the default branch (often 'master') you should see a link to compare:

Compare 1

Click it and you should get redirected to the compare-tool where you can select branches or commits to compare

Compare 2

UPDATE December 2021

GitHub seems to have hidden this option, and it is now behind the "Contribute" drop-down:

Screenshot of the "contribute" dropdown Screenshot after you click the "contribute" dropdown, showing the Compare button

maples
  • 196
  • 8
tkausl
  • 13,686
  • 2
  • 33
  • 50
  • OK, its interesting that I do not see that option on our team's GitHub portal and then when I sat down with our GitMaster, i realized that it is because the Git version that we are using is customized so we are not seeing this option. However, I can compare the two branches if I browse to the URL that's been mentioned below. The thing is that both of your answers are very useful but I can only choose one..Confused!! – Lost Apr 22 '17 at 23:45
  • 1
    It can currently (November 2021) be found under the "Contribute" button. – Radllaufer Nov 05 '21 at 06:45
  • @Radllaufer it seems like I can't see the Contribute button when there is an open PR, and if it's a PR for a different branch than the one I want to compare to that doesn't help as I can only view the PR. I may be missing something but it seems like this was a recent change because I didn't always have this problem. – regularmike Nov 05 '21 at 13:10
  • @regularmike you can still get there from a different branch and then select the branch with the open PR. Not sure if this solves your problem. Might be worth asking it as a question. – Radllaufer Nov 05 '21 at 13:50
  • @Radllaufer the solution I found in their docs was just to add /compare to the repo path and then you can choose two branches. – regularmike Nov 05 '21 at 18:27
38

Expanding on @Ari M's answer. URL format is as follows:

https://<REPO URL>/compare/<SOURCE BRANCH OR COMMIT>...<TARGET BRANCH OR COMMIT>

Note the difference between .. and ... (2 and 3 dots).

2 dots: show all commits that TARGET has but SOURCE doesn't and commits that SOURCE has but TARGET doesn't.

3 dots: show all commits that TARGET has but SOURCE doesn't. You usually want this.

E.g. to see what was added in the gh-pages branch compared to master in linguist repo:

https://github.com/octocat/linguist/compare/master...gh-pages

Max Ivanov
  • 5,695
  • 38
  • 52
  • Just a small note. If the branch name includes slashes it also works. Example: To compare `feature/JIRAID-123` and `feature/JIRAID-789` branches you can use `https://github.com/my-git-repo/compare/feature/JIRAID-123...feature/JIRAID-789` – elshev Jan 19 '22 at 13:55
  • I'd argue that 2 dots is more likely what people generally want when comparing branches other than master since 3 dots effectively compares with merge base. – Catskul Apr 04 '22 at 17:45
3

There is also another way to achieve this on GitHub, Just try to create a new Pull Request with the branches you would like to compare.

For example

branch-1 <- branch-2 or branch-2 <- branch-1

On the bottom, you can see the file and commit difference between those branches. Just don't Create the Pull request if you don't want to merge these two.

ABHi
  • 404
  • 4
  • 8
  • 1
    Here the point is to just compare without creating a PR so you can do it without changing anything on the repo. – Lost Jul 01 '20 at 00:16
2

I found the functionality almost at the top of the branch page. For me it says "This branch is 9 commits ahead of develop." with a link in the text. Clicking that link takes me to the compare branches-feature in github.com webpage.

link to github compare branches

It's not intuitive or obvious how to find this feature when it's hidden behind that link.

For future reference, the compare feature is located wherever the commits are located. Navigating to them will get you to the right location.

Emanuel Lindström
  • 1,607
  • 16
  • 25
1

This may be a useful tip that saves some typing: just adding /compare to the URL of yourBranch will compare against the default branch in the upstream repo, i.e. the one that you initially forked. Basically it will automagically generate a redirect to something like (note the inversion)

/compare/upstream...yourUserName:yourBranch

Also, to add to Max Ivanov's answer something that's obvious from the above, you can also specify other people's repo in the /compare, in the usual GitHub format userName:branchName.

Fizz
  • 4,782
  • 1
  • 24
  • 51
0

For us, compare option was disabled and git diff <branch-1> <branch-2> returns line by line differences and it confuses as well.

Intellij has a feature to compare with branch

Right click on project->Git>Compare with Branch.

List of files will appear having differences.

SudhirKumar
  • 366
  • 2
  • 4
  • 16