Git Tutorial: Learn Git Branching in 5 minutes

May 27, 2021 - 6 min read
Erin Schaffer
editor-page-cover

Git is one of the most widely used version control systems and is an important tool for every developer to know. One of the greatest benefits of Git is its branching capabilities. Git branching is a fundamental aspect of your version control workflow. Today, we’ll discuss how to create, delete, merge, and rebase Git branches. Afterward, we’ll cover the next steps you can take to further your Git knowledge.

We’ll cover:



Get hands-on with Git

Use live coding environments and real-world examples to learn one of the most widely used version control tools.

Learn Git the Hard Way

What is branching?

Imagine you’re working on a project with your team, and you’re creating a new feature that requires a lot of changes to the codebase. In the process, you discover a bug that needs to be fixed. This bug is related to your new feature, and you don’t want to affect your code. This situation could become complicated. Where will you store the code you’ve been working on?

That’s where Git branches come in. Branching is a core concept of source control that allows you to separate your work into different branches so you can work freely on your source code without affecting anyone else’s work or the actual code in the main branch.

With Git, you begin with a single primary branch called main. Any other commit you make after that will be made on the main branch, but Git allows you to create as many branches as you want.

Note: You don’t always have to create branches from the main branch. You can create a new branch from any other branch.

In your separate branches, you’re able to experiment and test without directly affecting your source code. Branching allows you and your team to switch between contexts without worrying about affecting different branches. It’s a great tool for teams because it makes collaboration easier, convenient, and flexible.

Note: Git branches are different from SVN branches. Git branches are useful in your everyday workflow, whereas SVN branches are used in large-scale development efforts.


Creating branches

A local branch is only accessible to you on your physical device. Before you can push a branch to a remote repository, you need to create a local branch first.

If you already have a local branch, you can use git checkout:

git checkout <name>

If you don’t already have the branch you need, you can create a new branch like this:

git checkout -b <name>

Now that we know how to create a new local branch, let’s take a look at how to create a remote branch.

Creating remote branches

You can push your local branch to a remote repo and it will become a remote branch. When a branch is remote, that means anyone with access to the remote repository now has access to the remote branch.

If you want to push your local branch to the remote repo, you can use git push:

git push -u origin <name>

Deleting branches

Now that we know how to create a local branch and a remote branch, let’s learn how to delete them. We’ll start with deleting a local branch using the -d flag:

git branch -d <name>

Sometimes, Git refuses to delete your local branch. This happens when your branch has commits that haven’t been merged into other branches or pushed to remote repositories. This refusal is meant to protect you from accidentally losing your data.

If you’re sure you want to delete your branch, you can use the -D flag. This flag forces deletion.

git branch -D <name>

Deleting remote branches

To delete a remote branch, you use the git push command:

git push origin --delete <name>

Keep the learning going.

Learn Git basics and advanced Git concepts without scrubbing through videos or documentation. Educative’s text-based courses are easy to skim and feature live coding environments - making learning fun and efficient.

Learn Git the Hard Way


Merging branches

Git merge is an essential function of branching. Merging allows you to join two or more branches through a commit. Only the target branch is changed. In our case, this would be the main branch. The history of your feature branch is preserved.

Let’s think back to the feature you’ve been working on with your team. Your whole team has access to the main branch that you uploaded to the remote repository. You’re working on a small fix to the feature, and you don’t want your work to affect the main branch.

You can create your own branch to experiment and test your changes. Once it’s completed and tested, you can merge that branch into the main so it’s accessible to everyone.

In our scenario, let’s say you want to merge your experimental branch, called myfeature, into the main branch. Before merging, you should do a git fetch to gather up-to-date information about the remote repository.

git fetch --all

Then, go into the branch you want to merge your changes into and perform git checkout.

git checkout main

Now, you can merge myfeature into the main branch.

git merge myfeature

You can check your project’s commit history on your repository to verify the merge. If your merge is successful and your work on myfeature is complete, you can delete that experimental branch since all the changes are now integrated into the main branch.

Rebasing branches

Git rebase is an advanced concept in Git. It can seem a bit complicated, but we’ll go over the basics. Rebasing and merging are pretty similar. Both options take commits from a branch and put them onto another branch.

The major difference between rebasing and merging is that rebasing erases the history of your feature branch after it transfers the work from the feature branch to the main branch. Some developers prefer this method because it ensures a simple review process.

Here’s how to use rebase:

git checkout myfeature
git rebase main

Git concepts to learn next

Congratulations on taking your first steps with Git branching! Many teams use Git, so it’s important to have a good grasp of the control system and its features. There’s still so much to learn about Git. Some recommended concepts to cover next are:

  • Cherry-pick

  • Advanced Git repository manipulation

  • Pull requests

  • Git commands

To get started with these concepts and more, check out Educative’s course Learn Git the Hard Way. This course takes you through Git basics to more advanced concepts. By the end, you’ll have a strong understanding of Git, which will serve you well throughout your career.

Happy learning!

Continue reading about Git


WRITTEN BYErin Schaffer

Join a community of 500,000 monthly readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.