How to Delete a Git Branch Locally and Remotely
When you are working with the team, and the whole team is making changes to the same code repository, Git comes into play for the version controlling system. Making new branches in Git is comparatively easier than other versioning control systems and deleting the local branches is also not a big deal anymore in Git.
This article will introduce how to delete a local or remote branch from Git. In most cases, when you start working for the first time with any repository, the remote name of the repository is origin
. So, If you pushed your code on the origin branch, you must delete it from the origin
branch. We have two types of branches in any versioning control system. The local branch is where every team member manages the work on their computer, and the remote branch is where all team members are working simultaneously on it.
Delete a Git Local Branch
First, we will see how to delete the branch from the local personal computer using the command line. To remove a local branch from the personal computer in Git, we have to run on the command line in the project directory.
git branch -d <branch-name>
The example above -d
parameter is used as an alias for delete, which is used to delete the particular branch if it is already fully merged with the remote branch. Whereas, if you want to force delete the branch, then -D
is used as an alias for force delete
, which deletes the branch whether it is merged or not in the remote branch.
Even If the branch has unmerged changes, Git will refuse to delete it. If you want to perform this action forcefully, then you have to force this delete action by replacing the -d
parameter with an uppercase -D
parameter:
git branch -D <branch-name>
we can sum up the local branch like this.
git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force-delete un-merged branches
Delete a Git Remote Branch
As mentioned above, we use Git for the local branch, but the case is different for the remote branches. We use git push
for the remote branch, even if that sounds a bit weird. In other words, we can say that we are pushing - sending - the order to delete the branch to the remote repository. Here’s how you do it:
git push --delete <remote branch name> <branch name>
In most cases, the remote name is origin
In certain cases, you’ll have to use the below command.
git push -d origin <branch_name>
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn