Learn Git

Git Branch

Branching in Git

In Git, there are common misconceptions about branching. For example, some assume that a branch is a group of commits, or that a branch must diverge in the Git graph.

In fact, a branch is simply a pointer to one specific commit.

Branch Pointer image

The branch pointer moves along with each new commit you make, and only diverges in the graph if a commit is made on a common ancestor commit.

The branching model offered by Git is lightweight compared to other version control systems, and helps protect you from merging unstable code into the main code base and gives you the chance to clean up your history before merging into the main branch.


How do I create a Git branch?

Creating a new branch in Git is quick and easy.

You can create a Git branch from any commit in your commit history; this can be great if you want to make changes starting from a previous point in your project.

How do I rename a Git branch?

You can easily rename a Git branch at any time.

How do I delete a Git branch?

If you decide you no longer need it, you can delete a Git branch to clean up your repository.

How do I switch a Git branch?

As the number of branches in your Git repository grows, there will likely come a time when you will be working on multiple tasks at the same time and will need the ability to move from one branch to another.

To switch to another branch in Git, you’re going to have to checkout the branch using the git checkout command.

Follow along with an example
using the GitKraken Git GUI

Branching in Git with GitKraken

Now, let’s go over how branching works when you’re using a Git client to visualize your repository.

In this example, we’re working with two Git branches: dev and production; to begin work on a new feature, we need to create a feature branch off the latest commit on the dev branch.

Creating a Git Branch in GitKraken

To create a Git branch using GitKraken, right-click on the target commit and select Create branch here from the menu.

Renaming a Git Branch in GitKraken

To rename a Git branch using GitKraken, simply right-click on the branch name and select Rename [branch name].

Deleting a Git Branch in GitKraken

To delete a Git branch in GitKraken, right-click the branch name from the graph and select Delete [branch name].

Ready for an easier way to do branching in Git?

The GitKraken Git client makes Git faster and more intuitive with its graphical user interface.


Additional Learning Git Resources: