Branching is a feature in Git that allows you to create independent versions of a project you can edit without affecting the main version of the project.
When you’re working with branches, you may decide you want to delete one. There are two approaches you can use to delete a branch in Git. Which one you use will depend on whether the branch you are deleting is located on your local machine or in a remote repository.
This tutorial will discuss, with reference to examples, how to delete remote and local branches in Git. By the end of reading this tutorial, you’ll be an expert at deleting local and remote branches.
Branching is an essential aspect of version control systems like Git. In Git, branches allow you to create a new version of an existing project to which you can make changes without affecting the original version of the project.
For instance, you could create a branch so you can work on adding a feature to the project. You could create another branch that stores the code for a bug fix you are working on.
Branching allows you to make changes to a codebase without changing the main version of the code until you are ready. So, if you have a bug fix stored on a branch, you can work on that bug fix for a few days until you are ready to incorporate it into the main version of your project.
If you’re interested in learning more about Git branches, read our beginner’s guide to the git branch command.
Deleting a Branch
You may decide to delete a branch for a number of reasons. For example, perhaps you’re done working on the branch, have integrated it into the main version of your project, and therefore no longer need it.
There are two approaches you can use to delete branches in Git; one is for deleting local branches, and the other is for deleting remote branches.
Delete a Local Branch
Local branches are Git branches stored on your local machine. The
git branch -d command allows you to delete a local branch that has been merged into your codebase.
Suppose we have a local branch called
fix-issue49 that we recently merged with the main version of our project. This branch contains a bug fix we were working on. Since we no longer need this local branch, we are ready to delete it.
It is not possible to delete a branch that you are currently viewing. Before deleting a local branch, you must first navigate to any other branch aside from the one you want to delete.
So, because we want to delete
fix-issue49, we first need to navigate to another branch. To do so, we can use the git checkout command.
The following command allows us to navigate to the master branch:
git checkout master
Now that we’re on the master branch, we can delete the local
fix-issue49 branch. We can do so using the following code:
git branch -d fix-issue49
-d flag indicates that we want to delete our branch.
fix-issue49 is the name of the branch we want to delete. When we run this command, Git deletes the local branch
If Git encounters any problems in deleting our branch, the deletion operation will stop.
You can use the
-D flag (note the capital letter) to force delete a local branch. The
-D flag will delete a branch regardless of whether or not you merged it to another branch in your codebase. Use the
-D flag with caution as it immediately deletes branches. Unless you are fully confident that you want to delete a branch, it is best to use the
Delete a Remote Branch
To delete a remote branch in Git, you can use the
git push <remote> --delete <branch> command.
Suppose we want to delete a branch called
fix-issue12. This branch is stored in our remote repository. Our remote repository has the reference name
origin. We can delete the
fix-issue12 branch by using the following command:
git push origin --delete fix-issue12
The above command, when executed, deletes the remote
fix-issue12 branch. After running this command, we should run a
fetch command to retrieve an up-to-date copy of all the branches stored on our remote repository. This will allow us to see, on our local machine, the changes made to our remote repository.
The following is what we would type into the program to fetch the branches on our remote repository:
git fetch -p
When you run this command, your local Git repository will fetch a copy of the remote repository and its branches. The
-p flag instructs Git to delete any local branches that no longer exist on your remote repository.
To learn more about the git fetch command, read our guide on git fetch.
Deleting a branch is a common operation in Git. Developers usually delete branches after they merge them with others in the repository.
git branch -d command allows you to delete a local branch. The
git branch <remote> --delete <branch> command allows you to delete a remote branch. In this tutorial, we discussed how to use these two commands to delete branches in Git. Now you’re equipped with the knowledge you need to start deleting branches like a Git pro!