163

Comparing histories on the same branch is very confusing for me on GitHub. I struggle with this regularly:

If I use compare/master in the URL after the GitHub repo name, I can compare against other branches in the repo based on the options in the drop-down menus.

https://help.github.com/en/articles/comparing-commits-across-time

However, I usually want to compare several commits on master.

How is this easily done? Could I get a more clear example?

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234

10 Answers10

291

You can use the following URL structure to compare commits in the same branch:

github.com/<username>/<repo_name>/compare/<commit1>...<commit2>

Replace values for username, repo_name, commit1(old commit) & commit2(new commit) acc. to your project.

The separator between the two commits is ... i.e. 3 dots.

Krishna
  • 6,107
  • 2
  • 40
  • 43
Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
  • 3 dots? That's the separator between the two commits? – AlikElzin-kilaka Nov 21 '18 at 12:33
  • 30
    Oh, it seems there's a [2-dot](https://help.github.com/articles/about-comparing-branches-in-pull-requests/#three-dot-and-two-dot-git-diff-comparisons) notations that shows more info. – AlikElzin-kilaka Nov 21 '18 at 12:42
  • 28
    This works fine I wonder why github did not have ui for it and we have to keep modifying urls – Ahmed Adewale Aug 28 '19 at 02:38
  • 1
    @lilhamad, There's kind of a UI. Not great, but it's something. https://stackoverflow.com/questions/49837769/how-to-compare-two-different-commits-on-the-same-branch-in-github/58942078#58942078 – wisbucky Nov 19 '19 at 20:10
  • 4
    @AlikElzin-kilaka, "3 dots" will compare from the last common commit: `git diff $(git merge-base A B) B`. "2 dots" directly compares the two commits: `git diff A B`. Similar to `git diff A...B` vs `git diff A..B` – wisbucky Nov 19 '19 at 20:19
  • 1
    @AhmedAdewale I find there are lots of very useful features that github can't find space for in the UI! – stevec Aug 23 '20 at 13:02
  • It seems username is optional, specially if you are already logged into your GitHub account – Ashutosh Chamoli Sep 21 '21 at 04:31
  • @stevec do you have some more examples of such features? – Nicofisi Jan 20 '22 at 16:20
  • @Ahmed Adewale since there is even an UI, I guess they just ran out of space in the menu and had to prioritize - "should we put a link to the extremely useful compare UI in the menu, or should we put that Insights thing that nobody cares in there?" – user1593842 May 13 '23 at 16:13
31

TLDR: Just add /compare at the end of the URL.

You can use the Github Compare UI, which will generate the URL for you. Replace ORG and REPO with your values. The UI only lists branches, but you can also type in any valid Tags (e.g. v1.0.0) or Commit IDs (e.g. 1a2b3c).

https://github.com/ORG/REPO/compare/

GitHub GUI showing compare options


The URLs that get generated are in this format. You can also manually edit the URL with the REFs.

https://github.com/ORG/REPO/compare/REF1...REF2

You can also use "2 dots" (direct diff) instead of "3 dots" (diff from last common commit). Same as git diff A..B vs git diff A...B.

https://github.com/ORG/REPO/compare/REF1..REF2

If you want to compare across forks, then you need to add ORG2:

https://github.com/ORG/REPO/compare/REF1...ORG2:REF2

There is documentation, but I didn't think it was that clear: https://help.github.com/en/github/committing-changes-to-your-project/comparing-commits-across-time

EJ Mak
  • 799
  • 1
  • 8
  • 29
wisbucky
  • 33,218
  • 10
  • 150
  • 101
4

The article you linked has instructions for comparing commits.

The GitHub comparison tool will accept any revision. Branches, tags, commit IDs, even dates. If you want to compare between two commits, give it the commit IDs. Here's their example.

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • How could one compare .27 to .28 for example? https://github.com/alpacahq/pylivetrader/tree/master/pylivetrader – gseattle May 12 '19 at 09:25
  • 2
    @gseattle https://github.com/alpacahq/pylivetrader/compare/v0.0.27..v0.0.28 – Schwern May 12 '19 at 18:22
  • Terrific and also all of alpacahq >= May 8 too? – gseattle May 14 '19 at 02:46
  • @gseattle Sorry, I'm not sure what you're asking for. You should ask it as a question. – Schwern May 14 '19 at 05:53
  • Since, in this example, pylivetrader has dependencies that might have also changed, can all changes throughout github.com/alpacahq be obtained using a date like on and/or after May 8? – gseattle May 14 '19 at 10:07
  • @gseattle They're separate repositories. You could search each of them individually using the [Github API](https://developer.github.com/v3/). But dependency management is better handled with a package manager like [bundler](https://bundler.io/). – Schwern May 15 '19 at 05:45
1

to see the difference between branches or tags

(branches if no pull request has been made)

Notes:

  • (if a pull request has been made you can see the changes inside the PR, no longer via the compare url)

  • You can use the dropdowns to select different branches or tags: enter image description here

  • take care of the order, eg. https://github.com/PyCQA/mccabe/compare/master...0.6.1 will give no results

  • you can choose between 2-dot (..) or 3-dot (...) notation

to see the difference between commits:

Kim Paulissen
  • 578
  • 5
  • 5
1

Just go to https://github.com/USERNAME/REPOSITORY_NAME/compare to achive the comparing page

enter image description here

Dyno Cris
  • 1,823
  • 1
  • 11
  • 20
1

Custom tabs.

Peeps, we can add a compare "custom tab" to the repository, so that we can quickly access this compare tooling.

Here is how.

  • Goto your repo landing page. https://github.com/<YOUR_ORG_NAME/<YOUR_REPO_NAME>
  • Click the "Settings" tab.
  • Click on "Custom tabs".
  • Fill in the "New tab" name.
  • Fill in the URL for compare. https://github.com/<YOUR_ORG_NAME>/<YOUR_REPO_NAME>/compare
  • Click the "Add tab" button

Now go to your repo landing page and you should now see your newly created custom tab along side the other default tabs (Pull requests, Settings and such)

enzotrout
  • 31
  • 4
0

If you need to compare changes within a branch but on different dates, this is how you can do it:

https://github.com/<ORG_NAME>/<REPO_NAME>/compare/HEAD..HEAD@{4 weeks ago}
https://github.com/<ORG_NAME>/<REPO_NAME>/compare/HEAD@{1 week ago}..HEAD@{2021-06-01}
dsaket
  • 1,827
  • 2
  • 19
  • 30
0

If anyone needs a handy UI, here you go. I made it for my projects, searching and comparing are quite tricky when I troubleshoot for cases. It helps to quickly create comparisons between the branches/tags/commits(hashes) in several clicks.

Chrome extension: GitHub Compare Online

https://chrome.google.com/webstore/detail/github-compare-online/dglncilbcfbjkdpiabohoofgkfabhpab

enter image description here

Neo Tan
  • 91
  • 6
0

I visit this page often and the top answer is great.

Today I learned you can not only use the commit SHA, but also the release tag!

Here's an example:

https://github.com/rails/rails/compare/v7.0.4...v7.0.5

stevec
  • 41,291
  • 27
  • 223
  • 311
0

Use ~N to compare with N prior commits

Tips like these are often hidden in the docs so I wanted to note it here where people will find it.


compare main 1 commit ago to main now

https://github.com/facebook/react/compare/main~1..main


compare 4 commits prior to this sha 0415b18

https://github.com/facebook/react/compare/0415b18~4...0415b18


DaveAlger
  • 2,376
  • 25
  • 24