This article was originally published on the LogRocket Blog on 27 March 2025.
To delete a local Git branch, you can use the command git branch -d branch_name for merged branches or git branch -D branch_name to force delete branches with unmerged changes.
For remote branches, the commands git push origin -d branch_name or git push origin -D branch_name can forcefully delete remote branches that have unmerged changes. These basic commands handle most of the branch deletion needs that developers encounter in their daily workflow.
Beyond these fundamental commands, this guide also explores more advanced branch management techniques, including the deletion of multiple branches at once, pruning remote branches with git remote prune origin, and recovering accidentally deleted branches using git reflog. We’ll also cover common errors you might encounter during branch deletion, their solutions, and best practices to maintain a clean, well-organized Git repository.
TL;DR
- Delete a local branch with
git branch -d branch_name, or force delete one with unmerged changes usinggit branch -D branch_name. - Delete a remote branch with
git push origin --delete branch_name. Deleting a local branch doesn't delete its remote branch, and the reverse is also true. - You can delete multiple branches in one command, or pipe
git branchthroughgrepandxargsto delete branches that match a pattern. - Prune remote branches with
git remote prune origin, and recover an accidentally deleted branch by finding its commit hash withgit reflog. - Before deleting a branch, check for unmerged changes, review its history and communicate with your team to avoid accidental data loss.
The Importance of Branch Management
Git branches are fundamental to modern software development, allowing teams to work on multiple features and fixes at the same time without interfering with each other’s work. While creating branches is a daily part of software development, knowing how to clean them up is equally important.
This is because as a project progresses, its repository can become cluttered with merged or outdated branches, leading to confusion for team members. Just as a well-organized workspace improves productivity, a clean repository with properly managed branches helps maintain clarity and efficiency in your development workflow.
Whether you’re working solo or as part of a team, this guide will help you learn the best practices for branch management, how to safely delete both local and remote Git branches, some of the common pitfalls to avoid when removing branches and how to recover from accidental branch deletions.
Local Branches vs. Remote Branches in Git
There are two types of branches in Git:
- Local branches — Exist only on your local machine and are used for your personal development work
- Remote branches — Stored in a remote repository, such as GitHub or GitLab, and are used for collaboration and code sharing with others
When you create a new branch in Git, it creates a local branch for you by default. If you want to share that branch with others, you have to push it to a remote repository. Pushing a local branch to a remote repository creates a remote branch of the local branch with the same name.
When to Delete Branches
Deleting branches is a common practice in Git, and it’s essential to keep your repository clean and organized. Here are some scenarios where you might want to delete a branch:
- Merged branches — After merging a feature branch into the main branch, you can delete the feature branch since its changes are now part of the main branch
- Abandoned branches — If you have a branch that is no longer needed or has been abandoned, you can delete it to reduce clutter in your repository
- Outdated branches — If a branch is based on outdated code or has been replaced by another branch, it’s a good idea to delete it to avoid confusion
- Temporary branches — If you created a branch for testing or experimentation purposes and no longer need it, you can delete it to keep your repository clean
Safety Considerations When Deleting Branches
Before deleting a branch, you must ensure that you’re not deleting any work that you might need in the future. Here are some safety considerations to keep in mind when deleting branches:
- Check for unmerged changes — Make sure that any necessary changes in the branch have been merged into the main branch or another branch before deleting it
- Collaboration — If you’re working on a shared repository, make sure to communicate with your team before deleting branches to avoid accidental data loss
- Permissions — Ensure that you have the necessary permissions to delete branches, especially when working on protected branches or in a shared repository
- Review branch history — Before deleting a branch, review its commit history to ensure that you’re not deleting any important changes or work
- Recovery plan — Have a recovery plan in place in case you accidentally delete a branch containing important changes
Now that you understand the importance of branch management and the safety considerations when deleting branches, let’s dive into the methods for deleting branches in Git.
Methods for Deleting Git Branches
When deleting a local branch, it’s important to know that the remote branch is not deleted automatically. You should also delete the remote branch separately if you no longer need it. The same goes for a remote branch; deleting it does not delete its local branch.
Deleting a Local Branch in Git
To delete a local branch in Git, you can use the git branch command with the -d flag followed by the branch name. The -d flag stands for “delete” and is used to delete the specified branch:
git branch -d branch_name For example, to delete a local branch named feature-branch, you would run the following command:
git branch -d feature-branch 
deleting a local branch in git
You can also use the flag --delete instead of -d to delete a local branch:
git branch --delete branch_name Deleting a Local Branch With Unmerged Changes
When deleting a branch with unmerged changes, Git will throw an error to prevent accidental data loss. However, if you’re sure you want to delete the branch, you can use the -D flag, which stands for “force delete,” to delete it:
git branch -D branch_name If you prefer to use the --delete flag to delete the branch, you can do so by including the --force flag. This will force the deletion of the branch:
git branch --delete --force branch_name Deleting a Remote Branch in Git
To delete a remote branch in Git, you can use the git push command with the --delete flag or -d flag followed by the remote branch name:
git push origin --delete branch_name # or git push origin -d branch_name For example, to delete a remote branch named feature-branch, you would run the following command:
git push origin --delete feature-branch The origin in the command refers to the remote repository where the branch is located.

deleting a remote branch in git
Deleting a Remote Branch With Unmerged Changes
To delete a remote branch with unmerged changes, you can use the --force flag with the git push command to force the deletion of the branch:
git push origin --delete --force branch_name Deleting Multiple Local Branches
In a case where you need to delete multiple branches at once, you can use the git branch command with the -d flag followed by the branch names separated by a space:
git branch -d branch_name_1 branch_name_2 branch_name_3 
deleting multiple branches in git
This will delete all the specified branches, one by one in a single command. To forcefully delete multiple branches, you can use the -D flag instead of -d:
You can also write a script to delete multiple branches that match a specific pattern simultaneously. For example, to delete all branches that start with feature-, you can run the following command:
git branch | grep 'feature-' | xargs git branch -d To delete all branches, excluding a particular branch, you can run the following:
# Delete all branches, excluding the main branch git branch | grep -v ‘main’ | xargs git branch -d To delete all branches, excluding multiple branches, you can run:
# Delete all branches, excluding main and develop git branch | grep -vE 'main|develop' | xargs git branch -d To delete only merged branches or only unmerged branches, you can use the flags --merged and --no-merged, respectively:
# Delete only merged branches, excluding the main branch git branch --merged | grep -v 'main' | xargs git branch -d # Delete only unmerged branches git branch --no-merged | xargs git branch -d Deleting Multiple Remote Branches
To delete multiple remote branches, you must include the -r flag in the git branch command, followed by the remote branch names separated by a space:
# Delete multiple remote branches git branch -r -d branch_name_1 branch_name_2 branch_name_3 # Force delete multiple remote branches git branch -r -D branch_name_1 branch_name_2 branch_name_3 # Delete all remote branches, excluding the main branch git branch -r | grep -v ‘main’ | xargs git branch -d # Delete all remote branches excluding main and develop git branch -r | grep -vE 'main|develop' | xargs git branch -d # Delete only merged remote branches, excluding the main branch git branch -r --merged | grep -v 'main' | xargs git branch -d # Delete only unmerged remote branches git branch -r --no-merged | xargs git branch -d Alternative Methods to Delete Branches
The command line is not the only way to delete branches in Git. You can also delete branches using Git GUI tools, such as GitKraken, Sourcetree, or GitHub Desktop. These tools provide a visual interface that makes it easy to manage branches, including deleting them.
You can also delete branches on GitHub or GitLab by navigating to the branches tab in your repository and selecting the branch you want to delete. From there, you can delete the branch with a single click.
You can also create actions or write scripts to delete branches automatically after they have been merged.
Pruning Remote Branches
Earlier, I mentioned that when you delete a local branch, the remote branch is not deleted automatically. If you have deleted several local branches, you may want to delete their corresponding remote branches. This process is known as pruning remote branches.
To view existing remote branches of local branches that have been deleted, use the git fetch command with the --prune flag:
git fetch --prune This will list all of them out in the terminal. You can then delete them with the following command:
git remote prune origin Recovering Deleted Branches
Don’t panic if you accidentally delete a branch! Git has a built-in mechanism to help you recover deleted branches. It keeps a record of all your branches’ commit history for a period of time, including the deleted ones. This allows you to recover a deleted branch and its commit history if needed.
To recover a deleted branch, you can use the git reflog. The git reflog command shows a log of all the actions you’ve taken in the repository and on all your branches, including deleted branches.
Run the command:
git reflog Then, from the output of the git reflog command, you can identify the commit hash of the branch before it was deleted. You can then use the git checkout command to recover the deleted branch:
git checkout -b branch_name commit_hash For example, to recover a deleted branch named feature-branch with the commit hash abc123, you would run the following command:
git checkout -b feature-branch abc123 
recovering a deleted branch in git
Common Errors and Solutions
When deleting branches in Git, you may encounter some errors. Here are some of the most common errors and their solutions:
-
error: The branch branch_name'is not fully merged— This error occurs when you try to delete a branch with unmerged changes. To resolve this error, use the-Dflag to force delete the branch -
error: Branch'branch_name'not found— This error occurs when you try to delete a branch that does not exist. Make sure you’re using the correct branch name and that the branch exists before deleting it -
error: unable to delete'branch_name': remote ref does not exist— This error occurs when you try to delete a remote branch that does not exist. Make sure you’re using the correct remote branch name and that the remote branch exists before deleting it -
error: Cannot delete branch'branch_name'checked out at'/path/to/branch'— This error occurs when you try to delete the branch you’re currently on. To resolve this error, switch to a different branch before deleting the branch
Best Practices for Branch Management
To maintain a clean and organized Git repository, it’s important to follow best practices for branch management. Here are some best practices to consider when managing branches in Git:
- Use descriptive branch names — Use descriptive branch names that reflect the purpose of the branch, such as
feature/add-new-featureorbugfix/fix-bug-123 - Regularly delete merged branches — Delete merged branches regularly to keep your repository clean and clutter-free
- Communicate with your team — Communicate with your team before deleting branches to avoid accidental data loss and ensure that everyone is on the same page
- Prune remote branches — Periodically prune remote branches to remove branches that have been deleted locally
- Automate branch deletion — Consider automating branch deletion using scripts or actions to delete branches after they have been merged
- Review branches regularly — Review the list of branches in your repository regularly and delete branches that are no longer needed
Conclusion
Proper branch management is essential for maintaining a healthy Git repository and improving your development workflow. In this guide, you’ve learned how to safely delete both local and remote Git branches and how to recover from accidental branch deletions. Regular cleanup of obsolete branches helps keep your workflow efficient and your repository organized.
By following the steps and best practices outlined in this guide, you can ensure that your repository remains clean and clutter-free, making it easier for you and your team to collaborate effectively. Remember to always check for unmerged changes, backup important changes, and communicate with your team before deleting branches to avoid accidental data loss.
If you're new to working with Git and GitHub, Getting Ready for Hacktoberfest: Preparing for Your Open Source Journey and Making Your First Open-Source Contribution: A Beginner's Guide are good next reads.
Give these commands a try the next time you clean up your repository, and tell me in the comments how it went. If it helped, share it with a teammate who has a few too many old branches.
Further Reading
- Getting Ready for Hacktoberfest: Preparing for Your Open Source Journey: set up Git, GitHub and GitHub Desktop on your machine.
- Making Your First Open-Source Contribution: A Beginner's Guide: create a branch, make your changes and open your first pull request.
- Hacktoberfest Etiquette and Best Practices: A Guide for First-Time Contributors: branch names, commit messages and other habits that make collaboration easier.
- How to Fork a Repo on GitHub Using Your Mobile Device: fork a repository straight from your phone.
- Elevating Software Development Through Collaborative Feature Management: how feature flags help teams ship features without stepping on each other.
Frequently Asked Questions
How do I delete a local branch in Git?
Run git branch -d branch_name to delete a merged branch. If the branch has unmerged changes, use git branch -D branch_name to force delete it.
How do I delete a remote branch in Git?
Run git push origin --delete branch_name or git push origin -d branch_name. The origin in the command refers to the remote repository where the branch is located.
Does deleting a local branch delete the remote branch?
No. Deleting a local branch doesn't delete its remote branch, and deleting a remote branch doesn't delete its local branch, so you need to delete each one separately.
How do I delete multiple branches at once?
List the branch names after git branch -d, separated by a space. To delete branches that match a pattern, like all branches that start with feature-, run git branch | grep 'feature-' | xargs git branch -d.
How do I recover a deleted branch in Git?
Run git reflog to find the commit hash of the branch before it was deleted, then run git checkout -b branch_name commit_hash to recover it.
Why can't I delete the branch I'm on?
Git won't delete the branch you're currently on, and gives you a "Cannot delete branch" error instead. Switch to a different branch first, then delete it.
What does git remote prune origin do?
It deletes the remote branches of local branches you've already deleted, a process known as pruning remote branches. Run git fetch --prune first to list them in your terminal.