Delete a local branch with git branch -d
To delete a local branch in Git, use the command git branch -d branch-name, replacing "branch-name" with the actual name of the branch you want to remove. This command only works if the branch has been fully merged into another branch — usually main or master. Git protects you by refusing to delete a branch that contains work not yet merged elsewhere.
Open your terminal or command prompt, navigate to your repository folder, and type the command. If the deletion succeeds, Git confirms it with a message like "Deleted branch feature-login (was abc1234)." The branch is now gone from your local machine.
Before you delete, make sure you are not currently on that branch. If you try to delete the branch you are actively working on, Git will refuse. Switch to a different branch first — usually git checkout main — then delete the one you left behind.
Key Takeaways
- Use git branch -d branch-name to delete a local branch that has been merged into another branch.
- Git blocks deletion of branches containing unmerged work to prevent accidental data loss; use -D (uppercase) only if you are certain the work is not needed.
- You cannot delete the branch you are currently on; switch to main or another branch first.
- Deleting a local branch does not affect the remote repository or other people's copies of the code.
- Use git branch -v to see all your local branches and confirm which one you want to delete before running the delete command.
Force delete a branch with unmerged changes
If Git refuses to delete a branch because it contains work that has not been merged, you have two options: merge the branch first, or force the deletion if you are certain the work is not needed.
To force deletion, use git branch -D branch-name (uppercase D instead of lowercase d). This bypasses Git's safety check and deletes the branch immediately, even if it has unmerged commits. Use this only when you are absolutely sure you do not need the work on that branch — once deleted this way, recovering it is difficult.
A common scenario: you created a branch to experiment, decided the approach was wrong, and want to throw it away without merging. In that case, git branch -D experiment-branch removes it cleanly. But if you might need that work later, merge it to main first, then delete it safely with the lowercase -d flag.
Check which branches you have before deleting
Before you delete anything, see what branches exist on your machine. Run git branch to list all local branches. The branch you are currently on appears with an asterisk (*) next to its name.
For more detail, use git branch -v to see each branch name alongside the most recent commit on that branch. This helps you confirm you are deleting the right one. If you want to see which branches have been merged into your current branch, run git branch --merged — these are safe to delete with the lowercase -d flag.
Conversely, git branch --no-merged shows branches with work that has not been merged yet. These will be refused by git branch -d unless you use the force flag.
Delete multiple branches at once
If you have several branches to clean up, you can delete more than one in a single command. Use git branch -d branch1 branch2 branch3, listing each branch name separated by a space. Git deletes them in order and confirms each deletion.
This is useful after a sprint or project phase when you have accumulated several feature branches that have all been merged. Instead of running the delete command five times, run it once with all the names.
If any of the branches cannot be deleted (because it contains unmerged work), Git stops and tells you which one failed. The branches listed before the failure are deleted; the ones after are not. You can then decide whether to merge the problematic branch or force-delete it separately.
Understand the difference between local and remote branches
A local branch exists only on your machine. When you delete it with git branch -d, it disappears from your computer but does not affect the remote repository (like GitHub or GitLab) or anyone else's copy of the code.
If you pushed the branch to the remote before deleting it locally, the remote branch still exists. Other people can still see it and pull it. To remove it from the remote as well, use git push origin --delete branch-name (replacing "origin" with your remote name if it is different).
In most workflows, you delete the local branch after merging and pushing, then clean up the remote branch separately — or let your hosting platform (GitHub, GitLab, Bitbucket) delete it automatically when you close a pull request. This two-step process prevents accidental deletion of work that others might still need.
Recover a deleted branch if you made a mistake
If you deleted a branch and immediately realized you needed it, recovery is possible but requires quick action. Git keeps a record of recent branch activity in the reflog. Run git reflog to see a history of branch operations, find the commit hash of the branch you deleted, and recreate it with git checkout -b recovered-branch commit-hash.
This works best if you act within a few days. The reflog is not permanent — Git eventually cleans up old entries. If you waited weeks, the branch may no longer be recoverable from your local machine. If you pushed the branch to the remote before deleting it, you can always pull it back from there with git checkout branch-name (Git will recreate the local branch from the remote).
The lesson: if you are unsure whether you need a branch, do not delete it yet. Branches are cheap to keep around, and the cost of recovering a deleted one is higher than the cost of leaving it alone.
Frequently Asked Questions
What happens if I delete a branch I have not pushed to the remote yet?
The branch and all its commits are removed from your local machine. If you have not pushed it anywhere, the work exists only on your computer. You can still recover it using the reflog if you act quickly, but waiting too long makes recovery harder. Always push important work to the remote before deleting the local branch.
Can I delete a branch that is currently checked out?
No. Git refuses to delete the branch you are actively on. Switch to a different branch first with git checkout main, then delete the one you left behind. This prevents you from deleting the branch your working directory is based on, which would cause confusion.
Does deleting a local branch affect my teammates?
No. Deleting a local branch only removes it from your machine. Your teammates still have their own copies of the branch on their computers and in the remote repository. If you want to remove it from the remote so others cannot see it, use git push origin --delete branch-name separately.
What is the difference between git branch -d and git branch -D?
The lowercase -d flag is safe: it refuses to delete a branch with unmerged work. The uppercase -D flag forces deletion regardless of merge status. Use -d by default and only switch to -D when you are certain the branch contains work you do not need.
How do I delete all branches except main?
There is no single command for this, but you can list merged branches with git branch --merged, then delete them in one command: git branch -d $(git branch --merged | grep -v main) on Mac or Linux. On Windows, delete them one at a time or a few at a time using the multi-branch syntax. Always review the list first to make sure you are not deleting something you need.