Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 753
ShanZhengYang Avatar asked Apr 15 '18 01:04

ShanZhengYang


People also ask

How do I compare two changes in GitHub branches?

Comparing branches is as easy as selecting the “compare to branch” option while perusing the feature branch you'd like to compare to another. The compare to branch option in GitHub Desktop is located under the “Branch” in the main menu at the top of the interface.

How do you compare different commits?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch.

Can I compare two files in GitHub?

GitHub only exposes the way to show diff between two commits. As an example, https://github.com/libgit2/libgit2sharp/compare/v0.9.0...v0.9.5 shows the diff between two versions of the LibGit2Sharp project. This diff includes all the modified files.


Video Answer


2 Answers

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.

like image 121
Pankaj Singhal Avatar answered Sep 20 '22 14:09

Pankaj Singhal


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

like image 45
wisbucky Avatar answered Sep 21 '22 14:09

wisbucky