Delete Local Git Branch

Last Updated on 2023-03-24

Git’s branch features allow developers to work on different parts of a project simultaneously. However, once a branch has served its purpose, it is often helpful to delete it to keep the repository clean and organized.

Using Warp’s Workflow features

If you are using Warp as your terminal and you want to quickly retrieve the command to delete a local branch, you can use Warp’s Workflows feature by pressing [.inline-code]CTRL-SHIFT-R[.inline-code], typing [.inline-code]delete local git branch[.inline-code], then pressing [.inline-code]ENTER[.inline-code] to use the suggested command:

[#delete-local-branch]Delete a local branch[#delete-local-branch]

  1. Start by checking the branch status

Before deleting a local branch, you should make sure that you are not on the branch you want to delete. To check the current branch status you can use the command:

 $ git branch

This will display a list of all the local branches and the current branch will be marked with an asterisk (*).

  1. Switch to a different branch

If you are on the branch you want to delete then you will need to switch to a different branch using the command:

 $ git checkout <branch-name>

You will often want to checkout the [.inline-code]main[.inline-code] branch at this point so you can replace [.inline-code]<branch-name>[.inline-code] with [.inline-code]main[.inline-code].

  1. Delete the local branch

Now that you are on a different branch you can safely delete the local branch using the command:

 $ git branch -d <branch-name>

This [.inline-code]-d[.inline-code] flag is an alias for [.inline-code]--delete[.inline-code] which can also be used.

  1. Verify the branch has been deleted

To verify that the local branch has been deleted then you can use the [.inline-code]git branch[.inline-code] command again to list all the active branches. You should thus see the deleted branch removed from the list.

[#delete-local-unmerged-changes]Delete a local branch with unmerged changes[#delete-local-unmerged-changes]

If the branch has changes that haven’t been merged with another branch or pushed to a remote repository, Git will throw an error message to protect you from losing important commit data. If you want to delete this branch regardless though you can use the `-D` flag which will force the deletion of the branch:

 $ git branch -D <branch-name>

This [.inline-code]-D[.inline-code] option is an alias for [.inline-code]--delete --force[.inline-code].

[#delete-all-local]Delete all local branches[#delete-all-local]

In some workflows, you may forget to delete branches after you have been merged into the [.inline-code]main[.inline-code] branch. This can leave you with many branches that you want to delete all at once. 

The [.inline-code]git branch -d[.inline-code] command can delete more than one branch at once, so you can pass in multiple branch names:

 $ git branch -d <branch-name-1> <branch-name-2> <branch-name-3>

Alternatively, if you want to delete all branches other than [.inline-code]main[.inline-code], you can use [.inline-code]regex[.inline-code] to identify the branch you don’t want to delete whilst deleting all the others:

 $ git branch | grep -v “main” | xargs git branch -D

Where [.inline-code]grep -v[.inline-code] finds all branch names that do not contain “main”, which are then passed to the [.inline-code]git branch -D[.inline-code] command.

[#local-remote-tracking]Delete a local remote-tracking branch[#local-remote-tracking]

A local remote-tracking branch is a reference to a branch on a remote repository that allows you to keep up to date with the changes on that remote branch. In some cases, you may not be interested in tracking the changes made in that remote branch, so you can stop your local repository from tracking those changes. To remove a specific remote-tracking branch you can use the [.inline-code]--remotes[.inline-code] or [.inline-code]-r[.inline-code] flag to delete the branch.

 $ git branch --delete --remotes origin/<remote-branch-name>
 $ git branch -dr origin/<remote-branch-name>

Of if you want to remove all remote-tracking branches you can do so with the [.inline-code]git fetch[.inline-code] command, while the [.inline-code]--prune[.inline-code] or [.inline-code]-p[.inline-code] option is enabled:

 $ git fetch origin --prune
 $ git fetch origin -p

Which remotes all obsolete local remote-tracking branches for any remote branch that no longer exists on the remote.