'How to compare two different commits on the same branch in github?

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?



Solution 1:[1]

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/

enter image description here


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

Solution 2:[2]

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.

Solution 3:[3]

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:

Solution 4:[4]

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

enter image description here

Solution 5:[5]

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)

Solution 6:[6]

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}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Smart Manoj
Solution 2 Dave Powers
Solution 3 Kim Paulissen
Solution 4 Dyno Cris
Solution 5 enzotrout
Solution 6 dsaket