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.

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.

If you want to create a Git branch using the terminal, you will use the git branch command, followed by your desired branch name. This will create a Git branch on your currently checked-out reference.

Learn more about how to create a branch in Git, including how to use one command to create and checkout your new branch simultaneously.

How do I rename a Git branch?

You can easily rename a Git branch at any time.

To rename a Git branch locally using the terminal, you will use git branch -m followed by the desired new branch name.

But if you are attempting to rename a branch that has already been pushed to a remote, you will need to push the new branch to the remote and update the upstream using the git push command with the -u (or --set-upstream) option.

See step-by-step how to rename a Git branch, including how to rename a local Git branch that you do not have checked out.

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.

To delete a Git branch using the terminal, you’re going to want to use the git branch -d command along with the name of the local branch you want to delete. Learn more about how to delete a Git branch locally.

To delete a remote branch in Git, you’re actually going to use the git push command. Learn more about how to delete a remote branch in Git.

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.

Branching in Git with GitKraken

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

It’s much easier to understand the fundamentals of branching in Git when you have a visual representation of your history.

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.

How do you create a Git branch with GitKraken?

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

How do you rename a Git branch with GitKraken?

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

How do you delete a Git branch with 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 Resources

Table of Contents

Make Git Easier, Safer &
More Powerful

with GitKraken

Visual Studio Code is required to install GitLens.

Don’t have Visual Studio Code? Get it now.