How to Delete a Local Branch in Git
Managing branches is one of the most routine parts of working with Git. Over time, local branches accumulate — feature branches that got merged, experiments that went nowhere, hotfixes that are long since deployed. Knowing how to delete them cleanly keeps your repository organized and reduces confusion, especially when working across a team.
What a Local Branch Actually Is
In Git, a local branch is a pointer that lives only on your machine. It exists in your .git directory and has no automatic connection to any remote (like GitHub, GitLab, or Bitbucket) unless you've explicitly set one up. Deleting a local branch removes that pointer from your local repository — it does not affect anything on the remote server.
This distinction matters. A lot of confusion around branch deletion comes from mixing up local branches and remote-tracking branches, which are different things even though they often share a name.
The Standard Command to Delete a Local Branch
The most common way to delete a local branch is with the -d flag:
git branch -d branch-name The lowercase -d is the safe delete option. Git will refuse to delete the branch if it contains commits that haven't been merged into your current branch or its upstream. This is a guardrail — it prevents accidental data loss.
If Git refuses with a message like "The branch is not fully merged," you have two choices:
- Merge or push the branch first, then delete it.
- Force delete it using the uppercase
-Dflag.
git branch -D branch-name The uppercase -D is shorthand for --delete --force. It removes the branch regardless of its merge status. Use it when you're confident the work is either already captured elsewhere or genuinely not needed.
🔍 Checking Before You Delete
Before deleting, it helps to confirm which branches exist and which have been merged:
git branch This lists all local branches. Your currently checked-out branch will have an asterisk next to it.
To see which local branches have already been merged into your current branch:
git branch --merged Branches that appear here are generally safe candidates for deletion with -d. To see unmerged branches:
git branch --no-merged These require more care — they contain commits that aren't yet in your current branch.
One Rule You Can't Skip
You cannot delete the branch you're currently on. If you try, Git will return an error. Switch to a different branch first — typically main or master — then run the delete command.
git checkout main git branch -d old-feature-branch Or with the newer switch syntax:
git switch main git branch -d old-feature-branch Local vs. Remote-Tracking Branches
When you clone a repository or fetch from a remote, Git creates remote-tracking references like origin/branch-name. These are stored locally but represent the state of the remote. Deleting a local branch does not remove these references, and it doesn't delete the branch on the remote server.
| What you're deleting | Command | Effect |
|---|---|---|
| Local branch | git branch -d branch-name | Removes local pointer only |
| Remote branch | git push origin --delete branch-name | Removes branch on remote server |
| Stale remote-tracking refs | git fetch --prune | Cleans up references to deleted remote branches |
If you're doing housekeeping after a merged pull request, you may want to run all three at different points in your workflow.
Variables That Affect Your Approach
How you handle local branch deletion in practice depends on several factors:
Merge strategy — Teams using squash merges or rebase workflows may find that -d frequently flags branches as "not fully merged" even when the work is clearly in the main branch. This happens because squash and rebase create new commit hashes, breaking Git's merge-detection logic. In those workflows, -D becomes routine rather than exceptional.
Solo vs. team workflow — On a personal project, deleting branches aggressively is low-risk. On a shared repository, it's worth confirming that your local branch matches the remote state before removing it, especially if others may have pulled from it.
Git version — The git switch and git restore commands were introduced in Git 2.23. If you're on an older installation, git checkout is the equivalent. The branch deletion commands themselves (-d and -D) have been consistent across versions for a long time.
IDE or GUI tools — Many developers delete branches through tools like VS Code's Source Control panel, GitKraken, or GitHub Desktop rather than the command line. These tools use the same underlying Git commands but surface them differently — sometimes with additional confirmation dialogs, sometimes without.
🧹 Bulk Deletion Patterns
For cleaning up multiple merged branches at once, a common pattern pipes git branch --merged into a delete command:
git branch --merged | grep -v "*|main|master" | xargs git branch -d This deletes all locally merged branches except your current branch, main, and master. The exact filter you'd use depends on your branch naming conventions and which branches you want to protect. Running git branch --merged first to review the list before piping to delete is a reasonable precaution.
Where Individual Setup Changes the Outcome
The mechanics of git branch -d and -D are consistent — they work the same way regardless of your operating system, hosting platform, or repository size. What varies is the workflow context around them.
Whether -d is sufficient or -D is needed regularly, whether you're cleaning up after every merge or doing quarterly housekeeping, whether you're working alone or coordinating with a team — these factors shape which approach fits your actual situation. The commands are the same; how often and when you reach for them depends entirely on how your project and team operate. 🗂️