Skip to main content
SysAdmin Shell Scripting Essentials

Git Delete Branch Locally: git branch -d and -D Commands

git delete branch locally removes a branch from your local repository using git branch -d (safe, merged only) or git branch -D (force, unmerged). Does not affect the remote or other clones.

git branch -d <branch-name>
git branch -D <branch-name>

Syntax

git branch -d <branch-name>
git branch -D <branch-name>
git branch --delete <branch-name>
git branch -d <branch1> <branch2>
git branch -D old-unmerged-branch

Tested on Ubuntu 22.04 with Git 2.34.1.

Options and Flags

Flag Type Default Description
-d Flag None Delete only if the branch is fully merged into its upstream or current HEAD. Aborts with error if unmerged.
-D Flag None Force delete branch regardless of merge status. Shorthand for --delete --force.
--delete Flag None Explicit delete command (same as -d).
--force Flag None Override merge check (used with –delete). Equivalent to -D.
-a Flag None List both local and remote branches (independent of delete).
-r Flag None List remote-tracking branches (not used for local deletion).
-m Flag None Rename a branch (not related to delete).
See also  Linux Show Routes CLI Command Reference: Syntax, Flags, Examples

Usage Examples

1. Delete a merged feature branch

git checkout main
git pull origin main
git branch -d feature/login-fix
# Output: Deleted branch feature/login-fix (was 3f4a9b1).

Standard post-merge cleanup: after merging via pull request, delete the local copy with -d to avoid discarding unmerged work.

2. Force delete an obsolete branch with unmerged changes

git branch -D experimental-v2
# Output: Deleted branch experimental-v2 (was a1b2c3d).

Use -D when you are certain the branch is no longer needed, even if it contains commits not present elsewhere. Those commits become unreachable unless referenced by another branch or tag.

3. Delete multiple local branches in one command

git branch -d old-feature-1 old-feature-2 old-hotfix
# Deletes each branch in sequence; stops on first error.

Saves time during cleanup. If any branch has unmerged commits, the command aborts. Use -D for unconditional bulk deletion.

Troubleshooting & Common Errors

Error Message Root Cause Resolution Command
Cannot delete branch 'main' checked out at '...' You are trying to delete the currently checked-out branch git checkout other-branch; git branch -d main
error: The branch 'feature' is not fully merged. Branch contains commits not merged into HEAD or upstream Use git branch -D feature to force delete, or merge first.
fatal: branch 'xyz' not found. Typo or branch does not exist locally Verify with git branch or git branch -a to list all branches.
error: cannot delete the branch 'master' which is the current HEAD You cannot delete the branch you are currently on Switch with git checkout <another-branch> then delete.

Performance Considerations

Deleting a branch is a metadata operation, usually instantaneous. In repos with thousands of branches or very large objects, internal Git memory limits may slow down due to ref updates. The configuration options below can help tune behaviour:

  • core.bigFileThreshold — Default 512 MiB. Increasing it reduces delta computations during branch deletion in repos with many large files.
  • core.deltaBaseCacheLimit — Default 96 MiB. Raising it speeds up object traversal when deleting a branch with many unique objects.
  • core.preloadIndex — Default true. Enables parallel index preloading, reducing checkout cost after deletion.
  • gc.auto — Default 6700. Frequent deletion sequences may benefit from manual git gc --auto to repack loose objects.
See also  Install tvservice on Raspberry Pi: Commands, Errors, and

View current values:

git config --get core.bigFileThreshold
git config --get core.deltaBaseCacheLimit
git config --get core.preloadIndex
git config --get gc.auto

Frequently Asked Questions

What is the difference between git branch -d and git branch -D?

Answer: -d deletes only if fully merged into its upstream or current HEAD; -D forces deletion regardless of merge status.

Use -d for safe deletion of merged branches. -D combines --delete --force, bypassing safety checks. Always verify with git branch --merged first.

git branch -d feature/logout
git branch -D feature/experiment

When should I use the --delete flag instead of -d?

Answer: Use --delete in scripts for readability; -d is faster interactively.

Both flags are identical. Choose --delete in CI/CD pipelines where consistency with --force matters. In daily terminal work, -d is standard.

git branch --delete old-branch
git branch -d old-branch

How do I fix error: The branch 'bugfix' is not fully merged?

Answer: Force delete with git branch -D bugfix, or merge the branch first, then retry git branch -d.

This error means the branch has commits not in HEAD. If safe to discard, use -D. To preserve commits, merge or rebase into the target branch.

git branch -D bugfix
# Or merge, then delete:
git checkout main
git merge bugfix
git branch -d bugfix

Does git branch -d work on all operating systems?

Answer: Yes, it is platform-independent — works identically on Linux, macOS, and Windows (Git Bash, PowerShell, WSL).

git branch -d feature/payment

What is the fastest way to delete multiple local branches matching a pattern?

Answer: Combine git branch, grep, and xargs for batch deletion by pattern.

git branch | grep '^  fix/' | xargs git branch -D
git branch --merged | grep 'feature-' | xargs git branch -d

See also  Powershell Updates — Verified Syntax, Flags & Troubleshooting

Closing Tip

In CI/CD scripts, always use git branch -d (not -D) to prevent accidental deletion of unmerged work. Reserve -D for interactive, manually reviewed cleanups.