Git: Delete a Local Branch
Last updated
Once a branch's work is merged, you delete it with git branch -d <branch>. Git refuses if the branch has commits that aren't merged anywhere - a safety check. To delete it anyway, use the capital -D. Deleting a local branch never touches the copy on the remote; that's a separate command.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git branch -d feature | Delete feature (only if it's merged) |
git branch -D feature | Force-delete feature even if unmerged |
git branch -d branch1 branch2 | Delete several branches at once |
Common cases
| Goal | Command |
|---|---|
| Delete a merged branch | git branch -d old-feature |
| Force-delete an unmerged branch | git branch -D scratch |
| Delete the branch you're on | git switch main then git branch -d feature |
| See which branches are merged | git branch --merged |
Worked example
Finish a feature, merge it, and clean up the local branch.
| Step | Command | Result |
|---|---|---|
| 1 | git switch main | Move off the branch you want to delete |
| 2 | git merge feature | Bring the feature's commits into main |
| 3 | git branch -d feature | Delete the now-merged branch |
Git delete local branch FAQ
What's the difference between git branch -d and -D?
-d is the safe delete: Git only removes the branch if its commits are already merged into another branch, so you can't accidentally lose work. -D is the force delete (shorthand for --delete --force): it removes the branch no matter what, even if it has unmerged commits. Use -d by default and reach for -D only when you're sure you want to throw the branch's commits away.How do I delete the branch I'm currently on?
git switch main (or git checkout main), then run git branch -d feature. If you try to delete the current branch, Git errors with "Cannot delete branch ... checked out".Does deleting a local branch delete it on GitHub?
git branch -d only removes the branch from your local repository. The branch on the remote (GitHub, GitLab, etc.) stays until you delete it separately with git push origin --delete feature.Can I recover a branch I deleted?
git reflog to find the commit the branch pointed at, then recreate it with git branch feature <commit-hash>. The commits aren't garbage-collected immediately, so a branch deleted by mistake can normally be restored.Can I practice this online?
Yes. Open the terminal playground to run these commands in a real shell in your browser - nothing to install. When you want structure, Coddy's free interactive Git course walks you from your first commit through branching and merging step by step.