How to delete a local branch in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Over time, you may accumulate local branches that you no longer need. Deleting these branches can help keep your repository clean and manageable. This guide will provide a comprehensive overview of how to delete a local branch in Git, along with other related operations.

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

To delete a local branch in Git, you can use the git branch command with the -d option, which stands for --delete. This is the safest method as it prevents you from deleting a branch with unmerged changes.

To delete a local branch using the -d flag, run:

Terminal
git branch -d <branch_name>

Replace <branch_name> with the name of the branch you want to delete.

To delete a branch named feature-x, you would run:

Terminal
git branch -d feature-x

If the branch has changes that haven't been merged yet, ie. if you have local changes that you have yet to push to the remote repository, Git will block the deletion and display an error message.

If you're sure that you want to delete a branch regardless of its merge status, you can use the -D option, which is a shorthand for --delete --force.

Terminal
git branch -D feature-x

This command forces the deletion of feature-x, even if it has unmerged changes.

In some cases, you might want to delete all local branches except the main branch. This is extreme but useful in certain scenarios like after completion of a project.

Terminal
git branch | grep -v 'main' | xargs git branch -D

This command lists all branches, filters out the main branch, and forcefully deletes the rest.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2

For further reading on Git branch management, see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2