How to Delete a Local Branch in Git

Managing branches is a core part of working with Git, and knowing how to clean them up is just as important as knowing how to create them. Local branches can accumulate quickly — feature branches, experiment branches, hotfix branches — and deleting the ones you no longer need keeps your repository tidy and your workflow clear.

What Is a Local Branch in Git?

A local branch exists only on your own machine. It's separate from a remote branch, which lives on a server like GitHub, GitLab, or Bitbucket. When you delete a local branch, you're only removing it from your local Git environment — the remote version (if one exists) stays completely untouched.

This distinction matters. Many developers accidentally assume deleting a local branch removes it everywhere. It doesn't. Remote branches require a separate deletion command.

The Basic Command to Delete a Local Branch

Git provides two flags for deleting a local branch, and which one you use depends on the state of that branch.

Safe Delete: -d Flag

git branch -d branch-name 

The lowercase -d flag is the safe delete option. Git will only delete the branch if it has been fully merged into its upstream branch or into your current HEAD. If there are unmerged commits on that branch, Git will refuse and throw a warning — protecting you from accidentally losing work.

Force Delete: -D Flag

git branch -D branch-name 

The uppercase -D flag is a force delete. It removes the branch regardless of its merge status. Use this when you're certain you no longer need the branch — for example, if you created an experimental branch that went nowhere, or if the changes were abandoned intentionally.

⚠️ Force-deleting a branch with unmerged commits means those commits are no longer reachable through any branch reference. Git's garbage collection will eventually clean them up, and while you can recover them temporarily using git reflog, that window is limited.

Step-by-Step: Deleting a Local Branch

Here's the typical workflow:

1. Check which branch you're currently on

git branch 

The branch with an asterisk (*) next to it is your active branch. You cannot delete the branch you're currently on. Switch to another branch first.

2. Switch to a different branch

git checkout main 

Or, using the newer syntax:

git switch main 

3. Delete the target branch

git branch -d branch-name 

Replace branch-name with the actual name of the branch you want to remove.

Common Errors and What They Mean

Error MessageWhat It MeansFix
error: Cannot delete branch 'X' checked outYou're currently on that branchSwitch to another branch first
error: The branch 'X' is not fully mergedBranch has unmerged commitsUse -D to force delete if intentional
error: branch 'X' not foundBranch name is misspelled or doesn't existRun git branch to list available branches

Deleting Multiple Local Branches at Once

If you've accumulated many stale branches, you can delete them in a single command by listing them:

git branch -d branch-one branch-two branch-three 

For a more automated approach, some developers use shell scripting or Git aliases to delete all branches that have already been merged into main:

git branch --merged main | grep -v "^* " | grep -v "^ main$" | xargs git branch -d 

This command lists all branches merged into main, filters out main itself and your active branch, then deletes the rest. Review the output of git branch --merged main before piping it to a delete command — especially in shared or complex repositories.

Local vs. Remote: Understanding the Scope

A common point of confusion is what happens on the remote after you delete a local branch.

🔍 Nothing. Deleting a local branch has zero effect on the remote. If you also want to remove the branch from a remote like GitHub, you need a separate command:

git push origin --delete branch-name 

Conversely, if a remote branch has been deleted by a teammate and you want to clean up the stale remote-tracking references in your local repository, run:

git fetch --prune 

This doesn't delete your local branches — it only removes the outdated references to remote branches that no longer exist.

Factors That Affect How You Should Approach Branch Deletion

The right approach varies depending on several factors:

  • Merge status — Whether the branch is fully merged determines whether -d or -D is appropriate
  • Team workflow — In collaborative environments, you may want to confirm with teammates before deleting shared-origin branches
  • Git version — Older Git versions may not support git switch; git checkout works universally
  • Repository size and age — Long-running projects tend to accumulate more stale branches, making batch deletion more relevant
  • CI/CD pipelines — Some pipelines are triggered by branch names; deleting a local branch won't affect CI, but it signals intent about where work stands

Whether you're maintaining a solo project or contributing to a large team repository, the specific branches worth deleting — and when — depends entirely on your current development stage, branching strategy, and how your team tracks completed or abandoned work.