Q&A
How to delete a local branch in git
Overview
A Git branch is just a lightweight, movable pointer to a commit. "Deleting a branch" simply removes that pointer (and Git's reflog entry) - the underlying commits remain until garbage-collected if no other reference points to them. Cleaning up merged feature branches keeps your repository tidy.
graph TD
A[Want to delete branch X] --> B{Currently on X?}
B -->|yes| C[git switch main first]
B -->|no| D{Is X fully merged?}
C --> D
D -->|yes| E["git branch -d X (safe)"]
D -->|no & you're sure| F["git branch -D X (force)"]
E & F --> G[Local branch pointer removed]
style E fill:#1e1b4b,color:#fff
Safe Delete: -d
git branch -d feature-login
The lowercase -d (--delete) is the safe option: Git deletes the branch only if its commits are already merged into your current branch (or its upstream). If not, Git refuses:
error: The branch 'feature-login' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-login'.
This guard protects you from accidentally losing unmerged work.
Force Delete: -D
git branch -D feature-login
The uppercase -D (shorthand for --delete --force) deletes the branch regardless of merge status, discarding any commits unique to it. Use it when you intentionally want to abandon a branch's work.
You Can't Delete the Current Branch
If you try to delete the branch you're standing on:
error: Cannot delete branch 'feature-login' used by worktree ...
Switch away first:
git switch main # (or: git checkout main)
git branch -d feature-login
Useful Variations
List branches before deleting
git branch # local branches (* marks current)
git branch --merged # branches already merged into HEAD (safe to delete)
git branch --no-merged # branches with unmerged commits
Delete multiple branches at once
git branch -d feat-a feat-b feat-c
Delete all merged branches (cleanup, except main/master)
git branch --merged | grep -vE '^\*|main|master' | xargs -r git branch -d
Deleting the Remote Branch (different command!)
Deleting a local branch leaves the remote one intact. To delete the branch on the remote (e.g. GitHub):
git push origin --delete feature-login
# older equivalent:
git push origin :feature-login
And to clean up stale remote-tracking references locally:
git fetch --prune
# or
git remote prune origin
Recovering a Just-Deleted Branch
Because commits aren't immediately destroyed, you can often recover a branch via the reflog:
git reflog # find the commit hash the branch pointed to
git branch feature-login <hash> # recreate the branch at that commit
This works until Git's garbage collection removes unreferenced commits (typically ~30–90 days by default).
Why This Matters in Interviews
This checks Git fluency: the -d vs -D distinction (safe vs force), the fact that you can't delete the current branch, and - a frequent trip-up - that local and remote deletion are separate operations (git branch -d vs git push origin --delete). Knowing recovery via git reflog shows real depth.
Common Pitfalls
- Using
-Dby habit and silently discarding unmerged work. - Expecting
git branch -dto also remove the remote branch (it doesn't). - Trying to delete the branch you're currently checked out on.
- Forgetting
git fetch --pruneto clear stale remote-tracking branches.
References
git branchdocumentation: https://git-scm.com/docs/git-branch- Pro Git book (free): https://git-scm.com/book/en/v2