How To Delete a Local and Remote Git Branch

By 

Updated on

4 min read

Delete a Local and Remote Git Branch

When you work with Git branches every day, finished feature branches and hotfix branches pile up quickly. Once a branch is merged, it serves no purpose except for historical research. Deleting it keeps the branch list clean and makes navigating the repository easier for everyone on the team.

This guide explains how to delete local and remote Git branches, clean up stale references, and recover a branch if you remove one by mistake.

Delete a Local Git Branch

The git branch command allows you to list, create , rename , and delete branches.

To delete a local Git branch, invoke the git branch command with the -d (--delete) option followed by the branch name:

Terminal
git branch -d branch_name
output
Deleted branch branch_name (was 17d9aa0).

If you try to delete a branch that has unmerged changes, you will receive the following error message:

output
error: The branch 'branch_name' is not fully merged.
If you are sure you want to delete it, run 'git branch -D branch_name'.

As the message suggests, you can force the deletion of a branch with the -D option, which is a shortcut for --delete --force:

Terminal
git branch -D branch_name

Keep in mind that if you delete an unmerged branch, you will lose all the changes on that branch.

Info
To list all the branches that contain unmerged changes, use the git branch --no-merged command.

If you try to remove the branch you are currently on, Git will refuse:

output
error: Cannot delete branch 'branch_name' checked out at '/path/to/repository'

Switch to another branch first, then delete the target branch:

Terminal
git switch other_branch
git branch -d branch_name

Delete a Remote Git Branch

In Git, local and remote branches are separate objects. Deleting a local branch does not remove the remote branch.

To delete a remote branch, use the git push command with the -d (--delete) option:

Terminal
git push remote_name --delete branch_name

Where remote_name is usually origin:

Terminal
git push origin --delete branch_name
output
...
 - [deleted]         branch_name

An older, alternative syntax for the same operation uses a colon prefix before the branch name:

Terminal
git push origin :branch_name

If someone else already deleted the remote branch, Git usually returns an error like this:

output
error: unable to delete 'branch_name': remote ref does not exist
error: failed to push some refs to 'git@example.com:/my_repo'

In that case, refresh your remote-tracking references by running:

Terminal
git fetch -p

The -p option tells Git to remove any remote-tracking references that no longer exist on the remote repository before fetching. You can also use git remote prune origin for the same result without fetching new data.

Delete Multiple Branches

You can pass several branch names to git branch -d in one command:

Terminal
git branch -d branch1 branch2 branch3

A common cleanup task is removing every local branch that has already been merged into main. You can do this by piping the output of git branch --merged to xargs:

Terminal
git branch --merged main | grep -v '^\*' | grep -v ' main$' | xargs -r git branch -d

This excludes the current branch and main itself. Review the list first by running git branch --merged main on its own before piping it to xargs.

Recover a Deleted Branch

If you delete a branch by mistake, Git does not immediately erase the commits. You can recover it using git reflog, which keeps a log of where HEAD has pointed recently:

Terminal
git reflog

Find the commit hash at the tip of the deleted branch, then recreate the branch from that commit:

Terminal
git branch branch_name commit_hash

The reflog entries are kept for 90 days by default, so act sooner rather than later.

Quick Reference

For a printable quick reference, see the Git cheatsheet .

TaskCommand
Delete a local branch (safe)git branch -d branch_name
Delete a local branch (force)git branch -D branch_name
Delete a remote branchgit push origin --delete branch_name
Delete multiple local branchesgit branch -d branch1 branch2 branch3
Delete all merged local branchesgit branch --merged main | grep -v '^\*' | grep -v ' main$' | xargs -r git branch -d
Remove stale remote-tracking refsgit fetch -p
Recover a deleted branchgit reflog then git branch branch_name commit_hash

FAQ

Can I undo a branch deletion?
Yes. Local branches can be recovered from the reflog as long as the commits still exist (typically within 90 days). Remote branches can be restored by pushing the recovered local branch back to the remote with git push origin branch_name.

Should I delete branches after merging?
It is recommended. Merged branches add clutter to the branch list and make it harder to find active work. Most teams delete the source branch immediately after a merge, and platforms like GitHub and GitLab offer an option to do this automatically.

Conclusion

Once a branch is merged, delete it locally with git branch -d and remotely with git push origin --delete. For more on managing remotes and branch names , check the linked guides.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page