98 Branches in GitHub

Written by Matthew Wankiewicz and last updated on 7 October 2021.

98.1 Introduction

In this lesson, you will learn:

  • What a branch of a GitHub repository is.
  • How to create a branch of a GitHub repository.
  • How to update a GitHub repository from your branched repository.

Prerequisite skills include:

  • Familiarity with GitHub repositories.
  • Have Git installed on your computer.
  • A GitHub account!

Highlights:

  • Branching allows multiple people to work on the same project and update the GitHub repository individually.
  • Branching allows you to take a copy of the main repo and work on it on your own before you make any changes to the main repo.

98.2 The content

98.2.1 About Branches

When you work in groups using GitHub projects, it becomes annoying to have one person add all of the code to the repo by themselves, copying and pasting code from group members. This is where branches come in handy. - Branching allows you to take a copy of the current GitHub repo and make changes on your own computer. Once you and your group have made changes to the code, you can add the changes back into your initial repo. - Branching can also be used when you want to work on one part of a project separately from the other areas.

98.2.2 How to Create a Branch

To make a branch from a GitHub repo, go to the main repo you plan to work on and click on the drop down menu that should say main. It should look like the image below.

Once you press on that menu, GitHub will have a text-box that says “Find or create a branch…,” type a new name for your branch, something like ‘newbranch1.’ Since that branch does not exist, GitHub will ask you if you want to create a branch called ‘newbranch1.’ Click on “Create branch: newbranch1” and it will create the new branch for you, this is shown in the image below.

98.2.3 How to make a pull request

A pull request allows the owner of the GitHub repo to look over your changes to make sure that it will fit in with the current repo and also to make sure that it doesn’t create any conflicts in your repo.

To make a pull request from your branch, you first need to make a change to your branched repo. Once you make a change to the branch there will be a yellow bar on your screen asking if you want to make a pull request. As you can see in the image below, there will be a green button, and once you click on that you can create a pull request.

Once you press the button, GitHub will tell you if there are any issues with merging that branch to your main repo. If there aren’t any issues, GitHub will have a checkmark and will say “Able to merge.” You can then add a title and comment to your pull request to tell the owner of the repo what you did. Once you’ve filled out a comment and title, you can press “Create a pull request.” Once you do this, it will send a notification to the owner of the repo, telling them that your edits are ready to be reviewed.

After you’ve sent your pull request, the owner of the GitHub repo can go to the repo’s page and click on the “Pull Requests” tab. This page will contain a list of pull requests, this is where the owner can select your request. Once the owner gets to the page of your pull request, they will see a button that says “Merge pull request” (similar to the image below).

Once the owner clicks on the green button, they will be asked again if they want to make that change, once they press it again, the change will be merged onto the main branch and they will see something similar to the image below..

98.2.4 Forks

Forks are similar to branches. Forks take a clone of the main GitHub repo and allows multiple people to work on a GitHub project.

98.2.5 Updating a Branched (or Forked) Repo

If someone in your group makes a change to the master repo, there is a way to update your branch so you can see the changes they made. When a change is made, the webpage for the branched repo will say that your repo is “1 commit behind main.” This means that there has been 1 change between what your branch looks like compared to what the master repo looks like.

If you want to update your branch, press on the “Changes” button. You will then be brought to a page that will say “main is up to date with all commits from branch. Try switching the base.” Click on “switching the base.” It will then tell you if you are able to merge your branch and if you are, you can click on “Create pull request” (title and comment your request) and submit a pull request.

Now, click on “Merge pull request,” then “Confirm merge” and your branch will be updated!

98.3 Questions

Video explaining how to make a branch: https://youtu.be/eSd-Ib14fFk

Video explaining how to make a pull request: https://youtu.be/Wpz6ynJwqUg

Video explaining how to update a branch: https://youtu.be/c3btGlDxPNg

98.4 Exercises

98.5 Common Mistakes & Errors

Sometimes you can encounter issues in GitHub such as two people attempting to change the same file in their pull request. These conflicts will be discussed in the next module, “Dealing with Conflicts.”

98.6 Next Steps

Now that you have some experience with branching GitHub repos, some next steps include:

98.7 Questions

  1. True or False, you can use GitHub in R without a GitHub account?
  1. True
  2. False
  1. True or False, changing a file on a branch changes the master file?
  1. True
  2. False
  1. Which drop-down menu in GitHub allows you to create a new branch?
  1. The “branches” menu
  2. The “contributors” menu
  3. The “main” menu
  4. None of the above
  1. What is the main use of a pull request?
  1. It allows the owner of the GitHub repo to look over your changes
  2. It creates extra work for you
  3. It creates extra work for the repo owner
  4. None of the above
  1. If there are no conflicts present, which command will the repo owner see to add your changes to the main file?
  1. “Add request”
  2. “Merge pull request”
  3. “Bring in pull request”
  4. “Replace pull request”
  1. What are forks in GitHub?
  1. Copies of another repo that do not change the initial one
  2. A file in a repo
  3. The coding language the GitHub files are comprised of
  4. A collection of a user’s favourite files
  1. True or False, changes made on a forked repo can be added to the master repo?
  1. True
  2. False
  1. True or False, once you have a forked repo you cannot update it?
  1. True
  2. False
  1. True or False, you can have multiple branches with the same name?
  1. True
  2. False
  1. True or False, you can add a title and comments to a pull request?
  1. True
  2. False