Commits
Learn and practice the functionalities of commits in GitHub API.
We'll cover the following
Overview
Commits are a version history of our repository. Snapshots of our repository are taken from time to time which show how the changes were made. Whenever we make any logical changes, we should make a new commit. Authors and timestamps are also included in the commits alongside the message and content. Git commits are made in the branch from which we are checked out. Therefore, it’s recommended to check git status
before running a commit. Any new changes should be staged before running a commit.
List commits
First, let's try to fetch all commits form the specified repository:
headers = {'Authorization': 'token {{ACCESS_TOKEN}}','Accept': 'application/vnd.github.v3+json'}url='https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/commits'response = requests.get(url, headers=headers)print(json.dumps(response.json(), indent=4))
Get a commit
This endpoint can be used to get a particular commit of the branch, tag, or SHA. Read access to the repository is required to get the commit. The ref
parameter in the URL in the code below refers to the SHA of a commit. We can get the SHA of a commit from our GitHub account or it can be copied from the output of the "List commits" endpoint above.
headers = {'Authorization': 'token {{ACCESS_TOKEN}}','Accept': 'application/vnd.github.v3+json'}url='https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/commits/<ref>'response = requests.get(url, headers=headers)print(json.dumps(response.json(), indent=4))
Compare two commits
The basehead
parameter in the URL specifies the name of the two commits to compare. This parameter expects these names in the {base}...{head}
format. An example of the value of this parameter is given below:
abcd1234efgh5678ijkl9012mnop3456qrst7890...1234abcd5678efgh9012ijkl3456mnop7890qrst
headers = {'Authorization': 'token {{ACCESS_TOKEN}}','Accept': 'application/vnd.github.v3+json'}url='https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/compare/<basehead>'response = requests.get(url, headers=headers)print(json.dumps(response.json(), indent=4))