MeshWorld India Logo MeshWorld.
Git Terminal Command Branch Management 4 min read

Delete Local Git Branch Using Terminal

Jena
By Jena
| Updated: Apr 26, 2026
Delete Local Git Branch Using Terminal

Feature branches accumulate quickly during active development. After merging pull requests, those branches serve no purpose and clutter your local repository. Regular branch cleanup is essential hygiene for maintainable Git workflows.

[!TIP] Real-World Scenario: You open a project you haven’t touched in three months. git branch returns 47 branches — most merged years ago. Finding the active branch becomes a scrolling exercise. Systematic deletion prevents this accumulation.

- `git branch -d ` — delete merged branch (safe) - `git branch -D ` — force delete unmerged branch - `git checkout main` — switch before deleting current branch - `git remote prune origin` — clean up remote-tracking branches

How do I delete a local Git branch safely?

Git prevents deleting the branch you’re currently on. Switch to another branch first:

# Switch to main (or master)
git checkout main

# Delete the merged branch
git branch -d feature/old-feature

The -d flag (short for --delete) only succeeds if the branch has been fully merged into your current branch. This safety mechanism prevents accidental loss of unmerged work.

Check which branches are merged

# List branches merged into current branch
git branch --merged

# List branches NOT merged (be careful with these)
git branch --no-merged

Delete multiple branches at once

# Delete several merged branches
git branch -d feature/login feature/signup feature/dashboard

# Or delete all merged branches except main and current
git branch --merged | grep -v "\*" | grep -v "main" | xargs -n 1 git branch -d

How do I force delete an unmerged Git branch?

Sometimes you need to delete a branch containing work you no longer want — perhaps an abandoned experiment or a superseded approach:

# Force delete regardless of merge status
git branch -D experiment/abandoned-feature

[!WARNING] -D permanently discards commits not present in other branches. Verify the branch contents first:

git log experiment/abandoned-feature --not main

This shows commits in the feature branch that aren’t in main. Review before deleting.

Alternative: Delete with confirmation

# Interactive deletion with confirmation prompt
git branch -d feature/xyz 2>&1 | grep "error" && read -p "Force delete? [y/N] " confirm && [[ $confirm == [yY] ]] && git branch -D feature/xyz

How do I clean up remote-tracking branches?

After deleting branches on the remote repository, your local Git still maintains references to them:

# List remote-tracking branches
git branch -r

# Remove references to deleted remote branches
git remote prune origin

# Or fetch with prune in one command
git fetch --prune

Complete cleanup workflow

# 1. Fetch latest remote state and prune deleted branches
git fetch --prune

# 2. Delete all local branches already merged to main
git checkout main
git branch --merged | grep -v "^\* main$" | xargs git branch -d

# 3. List remaining branches for manual review
git branch

Summary

CommandActionRisk Level
git branch -d <branch>Delete merged branchSafe
git branch -D <branch>Force delete any branchDestructive
git branch --mergedList merged branchesSafe
git remote prune originClean remote-tracking refsSafe
  • Always switch away from a branch before deleting it
  • Use -d (lowercase) for routine cleanup of merged branches
  • Reserve -D (uppercase) for intentional discard of unmerged work
  • Run git fetch --prune regularly to sync with remote branch deletions

FAQ

Can I delete the branch I’m currently on? No. Git requires you to checkout a different branch first. Attempting to delete the current branch produces: error: Cannot delete branch 'xyz' checked out at '/path/to/repo'.

How do I recover a deleted branch? If you deleted with -d after merging, the commits exist in the merge commit’s history. If you used -D on unmerged work, recovery depends on git reflog:

git reflog | grep feature/deleted-branch
git checkout -b recovery-branch <commit-hash-from-reflog>

Should I delete branches after merging? Yes. Merged branches serve no purpose and create visual noise. Delete them immediately after merge to maintain repository clarity.

Does deleting a local branch affect the remote? No. git branch -d only affects your local repository. Remote branches are deleted separately with git push origin --delete branch-name.