Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub API - how to compare 2 commits

Tags:

It's possible to get list of changed files between two commits. Something like that comparison between two commits in web version but using GitHub Api.

like image 714
Unlink Avatar asked Nov 14 '14 07:11

Unlink


People also ask

How do I find the difference between two commits in git?

To see the changes between two commits, you can use git diff ID1.. ID2 , where ID1 and ID2 identify the two commits you're interested in, and the connector .. is a pair of dots. For example, git diff abc123.. def456 shows the differences between the commits abc123 and def456 , while git diff HEAD~1..

How do I compare two branches in GitHub?

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 I compare two commits in bitbucket?

To compare revisions in Bitbucket Data Center and Server: From the sidebar, click Compare. In the Compare page, from both the Source and Destination dropdown, select any combination of branches, tags, or commits. Once selections are made, the comparison results display in a diff and a commits list tab.


1 Answers

The official commit comparison API is Compare two commits:

GET /repos/:owner/:repo/compare/:base...:head 

Both :base and :head can be either branch names in :repo or branch names in other repositories in the same network as :repo. For the latter case, use the format user:branch:

GET /repos/:owner/:repo/compare/user1:branchname...user2:branchname 

Note that you can use tags or commit SHAs as well. For instance:

https://api.github.com/repos/git/git/compare/v2.2.0-rc1...v2.2.0-rc2

Note the '...', not '..' between the two tags.
And you need to have the oldest tag first, then the newer tag.

That gives a status:

  "status": "behind",   "ahead_by": 1,   "behind_by": 2,   "total_commits": 1, 

And for each commit, information about the files:

"files": [     {       "sha": "bbcd538c8e72b8c175046e27cc8f907076331401",       "filename": "file1.txt",       "status": "added",       "additions": 103,       "deletions": 21,       "changes": 124,       "blob_url": "https://github.com/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",       "raw_url": "https://github.com/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",       "contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/file1.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db5e",       "patch": "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"     }   ] 

BUT:

  • The response will include a comparison of up to 250 commits. If you are working with a larger commit range, you can use the Commit List API to enumerate all commits in the range.

  • For comparisons with extremely large diffs, you may receive an error response indicating that the diff took too long to generate. You can typically resolve this error by using a smaller commit range.


Notes:

"same network" means: two repositories hosted by the same Git repository hosting services (two repositories on github.com for example, or on the same on-premise GHE -- GitHub Enterprise -- instance)

You can therefore compare two branches between a repo and its fork.
Example:

https://api.github.com/repos/030/learn-go-with-tests/compare/master...quii:master 
  • compare link
  • diff link

(this example compares a fork to its original repo, not the original repo to the fork: that is because the fork, in this case, is behind the original repo)

like image 87
VonC Avatar answered Oct 04 '22 07:10

VonC