In this article, we will see how to delete a Branch in Git using 2 easy methods. If you are working on Git Repository then you might be aware of branching strategy and concepts. If you don't then let me explain you in a very brief. Every developer working on a project usually keep all their code in the local branch and once it is ready for review then he/she creates a pull request to review his/her code. After review approval this code will be pushed to some development branch like dev for QA deployment. Then after successful QA deployment, dev branch gets merged with master branch for production deployment.
This is a very general branching strategy followed by a developer. It may change project to project and company to company depending on the use case. But the bottom point is that once deployment is completed then a developer might delete his local and remote branches as it was a temporary branch required till deployment. Only dev and master is the permanent branch which should be kept stable at all the times. So to delete a branch in git, you need to follow below given methods. More on GitHub docs.
How to Delete a Branch in Git
Also Read: How to Configure Git Username and Email Address in 2 Simple Steps
Method 1: How to Delete a Local Branch in Git
In normal cases, when you don't have any error or exception in local branch, then you can delete your branch by using
git branch -d <branch_name> command. But before that let's check the list of local branches we have in our repository. As you can see from below output currently we have two local branch(
feature) and one remote branch(
[root@localhost ~]# git branch -a * dev feature remotes/origin/dev
So we will go ahead and delete
feature branch by using
git branch -d feature command.
[root@localhost ~]# git branch -d feature Deleted branch feature (was 99268c5).
Now if we again check the list of branches, then we see
feature branch is not there. This confirms successful deletion of
[root@localhost ~]# git branch -a * dev remotes/origin/dev
Sometimes it might be possible that due to some incomplete merge or due to any exception above
-d option won't able to delete the branch. So in that case you can use
-D instead to forcefully delete the branch as shown below.
[root@localhost ~]# git branch -D feature Deleted branch feature (was 99268c5).
Method 2: How to Delete a Remote Branch in Git
Another method that we have is for deleting remote branches. In case you want to delete any remote branch then you need to use
git push origin --delete <remote_branch_name> syntax to delete a remote branch. Before that let's check the list of branches we have currently available in our repository. You can use
git branch -a command to check all the local and remote branches.
[root@localhost ~]# git branch -a dev * feature remotes/origin/dev remotes/origin/feature
As you can see from above output, we have two local branch(
feature) and two remote branch(
origin/feature) available in our repository. To delete
origin/feature remote branch, you need to use
git push origin --delete origin/feature command as shown below.
[root@localhost ~]# git push origin --delete feature To ssh://firstname.lastname@example.org:2022/p/sample-project/r/sample-project-repo3 - [deleted] feature
Now if you again check the list of branches using same git branch -a command then you will see remote
feature is not available now. This confirms the successful deletion of
[root@localhost ~]# git branch -a dev * feature remotes/origin/dev