Menu
Coddy logo textTech

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.

git branch -d removes only the branch pointer - the merged commits stay in main's history.

Syntax

CommandWhat it does
git branch -d featureDelete feature (only if it's merged)
git branch -D featureForce-delete feature even if unmerged
git branch -d branch1 branch2Delete several branches at once

Common cases

GoalCommand
Delete a merged branchgit branch -d old-feature
Force-delete an unmerged branchgit branch -D scratch
Delete the branch you're ongit switch main then git branch -d feature
See which branches are mergedgit branch --merged

Worked example

Finish a feature, merge it, and clean up the local branch.

StepCommandResult
1git switch mainMove off the branch you want to delete
2git merge featureBring the feature's commits into main
3git branch -d featureDelete 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?
You can't delete the branch you have checked out. Switch to another branch first with 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?
No. 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?
Usually yes, if it was recent. Run 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.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED