Practice Working with branches

Last updated: January 4, 2023

As you must be starting to realize, Git is a powerful tool. One of its greatest features is how easy it makes branching workflows.

What is a branch?

A branch is a pointer to a commit (under the hood, it is a small file containing the 40 character SHA-1 checksum of the commit it points to).

How to create a branch?

Initial branch (master)

When you run git init to initialize a repository, a branch gets created and it is called master (you could rename it to something else if you wanted—that initial branch, despite its name, has nothing special).

So as soon as you start working on your project, there is one branch (master ) and you are on it. Wait… what does this mean to be on a branch? There is another pointer, of a special nature, called HEAD that keeps track of where you are. And HEAD points to master .

To see this, you can run git log (or a more compact version git log --oneline ). You can see that HEAD points to master .

Since there are a lot of pointers, let's make this clear:

HEAD is a pointer to the branch master which is itself a pointer to a commit.

As you make commits, the master pointer and HEAD which points to it move automatically to the latest commit.

Additional branches

You can create an additional branch with:

git branch <branch-name>

Practice

Create a new branch called test .

This creates a new pointer to the commit you are on (so now, there are 2 pointers to that commit). But HEAD is still pointing to master , so you are still on the same master branch you've been on all along.

Practice

Run git log –oneline to see that you are still on the branch master , but that there is now a second branch called test .

How to switch branch?

To switch branch, you use a command we have already used many times to go to a previous commit: git checkout . What we were already doing then was moving HEAD around! Except that we were moving it between commits. This time, we are moving it between branches.

So switching branch is done with:

git checkout <branch-name>

Practice

Switch to your new branch test , then run git log –oneline again.

Creating a branch and switching to it immediately

When you create a branch, most of the time you want to switch to it. So there is a command which allows to create a branch and switch to it immediately without having to do this in two steps:

git checkout -b <name-new-branch>

This command is convenient: when you create a branch with git branch <branch-name> , it is easy to forget to switch to the new branch before making commits!

How to merge branches?

One thing that makes Git branches powerful is—as we just saw—how easy it is to create new branches and to switch from one branch to another. Another thing is how easy it is to merge branches together.

If you created an experimental branch and are happy with the result, you'll want to merge it into your main branch (usually master ).

  • First, switch to the main development branch (e.g. git checkout master )
  • Then, merge your experimental branch into the main branch:

git merge <branch-to-merge-into-current-branch>

Example:

If your main development branch is master and your experimental branch is called experiment , you'd run:

git checkout master
git merge experiment

Merge conflicts

As you were developing your experimental branch, maybe you were also developing your main branch. As long as the differences between the branches do not overlap (you have been working on different parts of the project in each branch, which can include different parts of the same file), there is no problem.

If the two branches contain different versions of the same part of a file however, Git cannot know which of the versions you want to keep. The merge will then be interrupted and Git will ask you to resolve the conflict before the merge can be completed.

We will talk about how to resolve merge conflicts in our Zoom session Collaborating through GitHub since such conflicts can also arise when you work with other people on a project.

Once the conflict is resolved, you can finish the merge with git commit (Git will automatically write the message, but you can edit it if you wish).

How to delete a branch?

Once you have merged a branch into another or if you decide that the experiments on a branch are not worth keeping, you can delete that branch with:

git branch -d <branch-name>

Example:

In our previous scenario, you'd run:

git branch -d experiment

Branch summary

Branches are a great way to experiment in projects in a safe way. If you don't like the result, you can simply get rid of the branch. If you like it, you can merge it with master .

# show all branches (current branch marked with *)
git branch

# create a new branch called <name>
git branch <name>

# checkout branch <name>
git checkout <name>

# create a branch called <name> and switch to it
git checkout -b <name>

# delete branch <name>
git branch -d <name>

Comments & questions