Delete local git branches with -d
May 24, 2022 ‐ 2 min read
We can delete a branch by using the
-d option to the
$ git branch -d <branch-name>
-d option is short for
Git does run some security checks before the local branch will be deleted. If git blocks your delete you can include the
--force flag in the above command as well.
There is a shortcut which combines the
-D (capital d). This option forces the branch to be deleted.
$ git branch -D <branch-name>
Do have some caution when force deleting branches in git, there is probably a good reason why git blocks you from deleting the local branch.
Delete the remote branch
Now that the branch has been removed from our local git repository we should keep our remote in mind as well. That is, we can remove the branch from our remote repository as well.
This might feel a bit unintuitive, but in order to remove the branch from our remote we should to our remote with the
--delete option. Assumed that our remote is named
origin we can remove the remote branch as follows:
$ git push origin --delete <remote-branch>
When you remove branches from a remote branch make sure that your team mates or CI/CD doesn't rely on the presence of this branch.