How can I diff two branches in GitHub?
GitGithubGit BranchGit Problem Overview
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?
Git Solutions
Solution 1 - Git
Use the compare feature to accomplish this.
> To compare different versions of your repository, append /compare
to your repository's path.
Solution 2 - Git
If you are on any branch other than the default branch (often 'master') you should see a link to compare:
Click it and you should get redirected to the compare-tool where you can select branches or commits to compare
UPDATE December 2021
GitHub seems to have hidden this option, and it is now behind the "Contribute" drop-down:
Solution 3 - Git
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
Solution 4 - Git
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.
Solution 5 - Git
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
.
Solution 6 - Git
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.