Deleting Git branches is common practice after you have merged a branch into your codebase. You can delete a Git branch on your local machine using the git branch -d flag. The git push origin –delete command removes a branch from a remote repository.
Branching lets you create independent versions of a project you can edit without affecting the main version of the project. When you are finished with a branch, you should delete it. This will help to keep your codebase clean.
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. You can make changes to the new branch 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. If you’re interested in learning more about Git branches, read our beginner’s guide to the git branch command.
Delete Branch in Git
You may decide to delete a branch for a number of reasons. Perhaps you’re done working on the branch and you have integrated the changes you have made into the main version of your project. Therefore, you no longer need the branch.
Git Delete Local Branch
You can delete a Git branch from your local machine using the git branch -d command. The -d flag denotes that you want to delete a branch.
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 in our local repository:
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
The -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 fix-issue49.
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 you merged it to another branch in your codebase.
Use the -D flag with caution as the flag immediately deletes branches. Unless you are fully confident that you want to delete a branch, it is best to use the -d flag.
Git Delete Remote Branch
To delete a remote branch in Git, you can use the command. This command instructs Git to push your local changes to the remote repository. In this process, Git deletes the branch you specify that you want to delete.
Suppose we want to delete a branch called fix-issue12. This branch is stored in our remote repository. Our remote origin refers to our remote repository. We can delete the fix-issue12 branch by using the following command:
git push origin --delete fix-issue12
The above command 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.
Developers usually delete branches after they merge them with others in the repository.
The git branch -d command allows you to delete a local branch. The 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!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more