Branch, Pull, Merge

Learning Competencies

By the end of this exploration, you should be able to:

  • Explain what a branch is and why we use them
  • Explain what a pull request is and why we use them
  • Understand the concept of merging branches

Summary

Shared from GitHub Guides

When you're working on a project, you're going to have many different features or ideas in progress at any given time – some of which are ready to go and others that are not. Branching exists to help you manage this workflow.

When you create a branch in your project, you're creating an environment where you can try out new ideas. Changes you make on a branch don't affect the main branch. This means you're free to experiment and commit changes, knowing that your branch won't be merged until it's ready to be reviewed by someone you're collaborating with.

Time Box

ActivityTime
Explore1 hour
Reflect20 minutes

Introduction

By default, all repositories have a main branch (previously people called this a 'master branch'). This is where the most perfect, well-tested code lives. The main branch should be the ideal version of your code and always be working/running correctly.

In cases where people want to make changes or add features, they will make a new branch off of the main. This will take a copy of the code from the main and allow you to make changes and test them out. Once they are thoroughly tested, they can be merged into the main branch.

When you work in teams, it's always good to have someone else review your code before it's merged into main. This is done through a pull request. A pull request is a friendly way of saying: "Hey! I'm done implementing this feature. Can you review my code?". When working in a team, it is best practice to have at least one other person review your pull request, and they should be the person to merge it once the changes are approved.

Branching is important because it helps to avoid accidentally overwriting other people's code, and also to avoid adding bugs into your "good" code. IE, the code on your main branch. Which, theoretically, won't have bugs in it.

Creating a Branch

You can create a branch via the command line or on GitHub itself. On the command line, you would do this by:

// On the main branch
git branch branch-name
// We're still on the main branch, though. To change over:
git checkout branch-name

You can also create a branch AND switch to it, (AKA "check it out") in one command line this:

// On the main branch
git checkout -b branch-name
// Now we've created and checkout out the branch-name branch

When using your command line, you can see your local branches (the branches on your local computer) by simply typing:

git branch

If you want to see your local and remote branches (your most common 'remote' will be Github), type:

git branch -a

Explore

Explore and try out Git branching online by clicking around these links for an hour or so. You'll use what you learn in an upcoming challenge.

Reflect

Open your text reflections file and answer the following.

  • What is main?
  • Why create a branch?
  • What commands do you use to interact with branches? How do you create them and switch them?