Branches

Learn and practice the branch operations using GitHub API.

Overview

Branches are separate areas within a repository where we can perform multiple tasks. For example, we can code in isolation and debug codes. We can also work on a specific branch without affecting the code in other branches. There is one default branch in a repository from which we can have multiple branches.

List branches

As there can be numerous branches of a single repository, it becomes very difficult to remember all of them. Therefore, the feature of listing a branch is quite helpful. The following code shows how we use the GitHub API to list the branches of a particular repository:

Press + to interact
headers = {
'Authorization': 'token {{ACCESS_TOKEN}}',
'Accept': 'application/vnd.github.v3+json'
}
url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/branches'
response = requests.get(url)
print(json.dumps(response.json(), indent=4))

Get a branch

We can get a particular branch from a repository of the specified user. We need to specify the branch name in the URL.

Let's specify the branch name in the place of <branch-name> in the code below and run it to fetch the details of the specified branch.

Press + to interact
headers = {
'Authorization': 'token {{ACCESS_TOKEN}}',
'Accept': 'application/vnd.github.v3+json'
}
url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/branches/<branch-name>'
response = requests.get(url)
print(json.dumps(response.json(), indent=4))

Rename a branch

We can use the GitHub API for branch operations like renaming branches. The users with admin rights or the permission to change the branch name can rename the branch. Whenever a branch is renamed, the URLs in the other branches that redirect to the changed branch are automatically updated.

Let’s run the following code to rename a branch. Replace the <branch-name> to be renamed in the URL with the branch’s name in line 6. The new name of the branch is sent as a parameter and is specified in line 9.

Press + to interact
headers = {
'Authorization': 'token {{ACCESS_TOKEN}}',
'Accept': 'application/vnd.github.v3+json'
}
url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/branches/<branch-name>/rename'
data = json.dumps({
'new_name': 'develop'
})
response = requests.post(url, headers=headers, data=data)
print(json.dumps(response.json(), indent=4))

Merge a branch

Working on a separate branch does not mean that we can’t link two branches. There will be cases when we take a chunk of code from a default branch and move to another. Then, we’d want to make changes in the other branch and merge the changed code back to the default. We can use the merge operation to merge the chunks of code of multiple branches.

Name

Type

Present In

Description

base

string

body

This specifies the name of the branch that the head will be merged into.

head

string

body

This specifies the branch name or a commit SHA1 to be merged.

commit_message

string

body

This is the message used when a merge commit occurs. If no message is specified, the default message is used.

Let’s try to merge two branches using the GitHub API. The names of the two branches to be merged have been specified in lines 9 and 10. Some important parameters for the endpoint of merging a branch are given below:

Press + to interact
headers = {
'Authorization': 'token {{ACCESS_TOKEN}}',
'Accept': 'application/vnd.github.v3+json'
}
url = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/merges'
data = json.dumps({
'base': '<base-name>',
'head': '<head-name>'
})
response = requests.post(url, headers=headers, data=data)
print(json.dumps(response.json(), indent=4))