Delete branch in git
Deleting branches in Git is a routine task that keeps your repository organized. It is important to understand proper commands and their options to avoid errors and warnings.
In this article, we will understand how to delete branches in git from both local system and remote repository with examples.
Deleting local branch
If you want to delete a branch that is on your local system with git, then use below command
git branch -d branch_name
Replace branch_name
with the name of the branch you want to delete.
Remember that the branch to be deleted should not be the current selected branch.
To delete the branch, first switch to another branch using git checkout other-branch-name
and then execute the delete command.
Possible Scenarios
You might encounter following errors or scenarios while deleting a local branch.
1. Error: The branch ‘branch_name’ is not fully merged
This error occurs when you try to delete a branch that hasn’t been merged into the current branch.
To resolve this, either merge the branch into the current branch and then delete it or force delete it using -D option as
git branch -D branch_name
2. What if the branch has some local changes?
If the branch to be deleted has local changes that are not committed yet, Git will not allow you to delete the branch.
There are two options:
a. Stash the changes
Use git stash
to temporarily save your local changes. Then delete the branch using the above command.
Afterward, you can apply the stashed changes using git stash apply
.
b. Commit the changes
If you want to keep the changes permanently, commit them using git commit
before deleting the branch.
3. What if there are merge conflicts?
If there are merge conflicts, git will not allow you to delete it.
You need to resolve the conflicts first by manually editing the conflicting files, marking them as resolved with git add
, and then committing them.
Once the conflicts are resolved, you can proceed with the branch deletion.
Deleting remote branch
Note that deleting a branch locally will not automatically delete it from remote repository. You have to delete it on your own.
For this, use below command
$ git push origin --delete branch-name
This command will delete the remote branch named branch_name
from the remote repository named origin
.
You can also use shortcut -d as below
git push origin -d branch-name
If your git version is older than 1.7.0, then below command would be appropriate
git push remote_name:branch_name
Possible Scenarios
You might encounter following errors or scenarios while deleting a remote branch.
1. error: unable to delete ‘branch_name’: remote ref does not exist
This error occurs when you try to delete a branch that doesn’t exist in the remote repository.
2. What if someone pushes to the branch after you’ve executed the delete command?
If someone commits and pushes to the branch just deleted, git will not restore the branch.
It is important for the team to ensure that they are aware of the branch deletion to avoid confusion and potential issues.
Conclusion
To summarize, branch deletion is a mandatory activity in order to keep both local and remote repositories clean.
Below are the commands for this.
Deleting a local branch
$ git branch -d branch-name
Force deleting local branch
$ git branch -D branch-name
Deleting a remote branch
$ git push origin --delete branch-name
Prior version 1.7.0
git push remote_name:branch_name
Hope the article was useful.