Skip
Arish's avatar

19. Deleting Branches


Deleting Branches

Once a feature is merged or a branch is no longer needed, you should delete it to keep your repository clean.

Delete Local Branches

Safe Delete (-d)

bash
1# Delete a merged branch
2git branch -d branch-name

Git will only delete the branch if it has been merged:

bash
1git branch -d feature-complete
2# Deleted branch feature-complete (was abc1234).

If not merged, Git will warn you:

bash
1git branch -d unmerged-branch
2# error: The branch 'unmerged-branch' is not fully merged.
3# If you are sure you want to delete it, run 'git branch -D unmerged-branch'.

Force Delete (-D)

bash
1# Force delete (even if not merged)
2git branch -D branch-name

⚠️ Warning: This permanently deletes the branch and its unique commits!

bash
1git branch -D abandoned-feature
2# Deleted branch abandoned-feature (was def5678).

Delete Remote Branches

Method 1: git push --delete

bash
1git push origin --delete branch-name

Method 2: git push with colon

bash
1git push origin :branch-name

Example

bash
1# Delete remote branch
2git push origin --delete feature-old
3
4# Output:
5To github.com:user/repo.git
6 - [deleted]         feature-old

Delete Both Local and Remote

bash
1# Delete local
2git branch -d feature-complete
3
4# Delete remote
5git push origin --delete feature-complete

Clean Up Stale Remote References

After remote branches are deleted, clean up local references:

bash
1# Prune stale remote-tracking branches
2git fetch --prune
3
4# Or prune for specific remote
5git remote prune origin

See Stale Branches

bash
1# Show remote-tracking branches that no longer exist on remote
2git branch -vv | grep ': gone]'

Cannot Delete Current Branch

You cannot delete the branch you're currently on:

bash
1git checkout feature-xyz
2git branch -d feature-xyz
3# error: Cannot delete branch 'feature-xyz' checked out

Solution: Switch to another branch first:

bash
1git checkout main
2git branch -d feature-xyz
3# Deleted branch feature-xyz

Delete Multiple Branches

One by One

bash
1git branch -d branch1 branch2 branch3

Using Pattern (with grep)

bash
1# Delete all feature branches
2git branch | grep 'feature/' | xargs git branch -d

Delete All Merged Branches

bash
1# Delete all merged branches except main
2git branch --merged | grep -v "main" | xargs git branch -d

Best Practices

1. Merge Before Deleting

bash
1# Ensure branch is merged
2git checkout main
3git merge feature-complete
4git branch -d feature-complete

2. Verify Before Force Delete

bash
1# Check what commits would be lost
2git log main..unmerged-branch
3
4# If you're sure
5git branch -D unmerged-branch

3. Clean Up Regularly

bash
1# Weekly cleanup script
2git fetch --prune
3git branch --merged | grep -v "main" | xargs git branch -d

Common Scenarios

Delete Feature After PR Merge

bash
1# After PR is merged on GitHub
2git checkout main
3git pull origin main
4git branch -d feature-complete
5git fetch --prune

Delete Abandoned Experiment

bash
1# Force delete abandoned work
2git branch -D experiment-failed

Clean Up After Sprint

bash
1# Delete all merged feature branches
2git branch --merged main | grep -v "main" | xargs git branch -d

Summary

TaskCommand
Safe delete (merged)git branch -d name
Force deletegit branch -D name
Delete remotegit push origin --delete name
Prune stalegit fetch --prune

Practice deleting branches in the next exercise!