How To Delete A Local Branch In GIT?
Being the most popular version control software in the world, Git is very good at providing developers with easy commands to manage branches of their Git repositories. A lot of the time, developers need to delete a local branch to clean their git repositories.
Luckily, this is very easy to do in Git.
To delete a local branch in git you need to use the git branch -d command.
bashgit branch -d your-branch
The rest of the article will explore the delete branch operation, as well as show you what to do in different situations that you may encounter while deleting a branch.
Let's get to it 😎.
How to delete a branch in git?
To delete a local branch, you need to use the git branch -d command.
bashgit branch -d your-branch
This command might fail if you have changes that haven't been merged to a local or remote branch. You will need to merge the branch first, and then repeat the command.
If, however, you want to force delete a branch that hasn't been pushed or that has unmerged changes, you can use this command.
bashgit branch -D your-branch
Your branch is now deleted, but only locally. If you want to delete the branch remotely, you will need to use another command.
How to delete a remote git branch?
To delete a branch remotely you will first need to find out your remote repository name.
The remote name is usually origin, but it can also be different...
You can delete a remote branch, by using the git push command. (I assume that the remote name is origin)
bashgit push origin --delete your-branch
The branch is now deleted from your remote repository 😁.
But, what to do if you don't remember the branch names that you have? In that case, you will need to list which branches exist both locally and remotely and then remove the unnecessary ones.
How to list all git branches?
Sometimes, we might have too many branches in our git repository to remember. That's where the git branch command becomes handy.
Use the git branch command to list all local branches.
bashgit branch
To list all remote branches, use the git branch -r command.
bashgit branch -r
To list both local and remote branches, use the git branch -a command.
bashgit branch -a
Now that you have listed all the local branches, you can delete the ones that you do not need.
But, what if you want to delete all your local branches? This can be done using the bash grep command.
How to delete all local git branches?
Sometimes, you have so many locale git branches, that it's just easier to delete them all.
Use a combination of the git branch --merged command with the bash grep command to delete all the merged branches.
Proceed with caution! This command will delete a lot of branches.
bashgit branch --merged | grep -v \* | xargs git branch -D
This command will delete all the branches that were merged.
If, however, you want to delete all the branches that are not present on the remote server you will need to use a different command.
How to delete branches that do no longer exist on the remote server?
To delete branches that are no longer present on the remote server, you will need to first, use this command.
bashgit remote prune origin
It will remove local references to remote branches that no longer exist.
Then, you will need to use this command (I know it's a scary command).
bashgit fetch -p && git branch --merged | grep -v '*' | grep -v 'master' | xargs git branch -d
This command will delete all local branches that are not on the remote server and that are merged.
How to fix the 'Cannot delete branch' error?
To fix this annoying error you need to follow those steps:
- Check if you are on the same branch that you are trying to delete... If you are, you need to switch branches, because you cannot delete a branch that you are currently on.
- Check if you have created multiple worktrees... If you did, you will need to run git prune, before deleting the branch. You can also remove the .git/worktree/ folder and try to delete the branch again.
How to revert a deleted branch?
So you made an error. You deleted a branch that you weren't supposed to. Don't panic, it may be not as bad as it seems... In most cases, you can revert a deleted branch by going into your git reflog.
First, you need to go into your git reflog.
bashgit reflog
Then, find the last commit sha of the deleted branch and restore it with this command.
bashgit checkout -b <branch> <sha>
This command will restore your branch to its latest commit.
Final thoughts
As you can see deleting a branch in git is very easy, but you need to always triple-check that you are deleting the branch that you actually want to remove. Luckily when you make a mistake you can always go into your git reflog and revert it.
If you are interested in learning more about git, I have written a few tutorials on it.
Thank you for reading this article, I hoped you've enjoyed it and you understand git a little bit better.
Please share this article with your fellow coders.