Deleting Branches
Learn about Deleting Branches
After merging a feature branch into main, the branch has served its purpose. Keeping old branches around clutters your repository. This episode covers how to delete branches safely, when to keep them, and how to manage branches on remote repositories like GitHub.
Deleting Local Branches
# Delete a branch (only if it's been merged)
git branch -d feature/login
# Deleted branch feature/login (was a1b2c3d).
# Force delete (even if NOT merged — use with caution)
git branch -D experiment/risky-idea
# This permanently deletes the branch pointer.
# The commits may still be recoverable via reflog for 30 days.
The -d flag is a safety check — Git won't let you delete an unmerged branch because you'd lose those commits. Use -D only when you're sure you don't need those changes.
Deleting Remote Branches
# Delete a branch on the remote (e.g., GitHub)
git push origin --delete feature/login
# or
git push origin :feature/login
# After deleting remote branches, clean up stale references locally
git fetch --prune
# or
git remote prune origin
Checking Merged Status
# List branches that HAVE been merged into current branch
git branch --merged
# These are safe to delete
# List branches that have NOT been merged
git branch --no-merged
# Think twice before deleting these
# Check against a specific branch
git branch --merged main
Bulk Cleanup
# Delete all local branches that have been merged into main
git branch --merged main | grep -v "main" | xargs git branch -d
# Delete all local branches except main and develop
git branch | grep -v "main\|develop" | xargs git branch -d
Branch Lifecycle (Best Practice)
- Create — Branch off
mainfor each feature or fix - Develop — Make commits on the feature branch
- Push — Push to remote for collaboration and backup
- Pull Request — Open a PR for code review
- Merge — Merge into
mainafter approval - Delete — Remove the feature branch (local and remote)
# The complete workflow:
git switch -c feature/new-thing # 1. Create
# ...make changes and commit... # 2. Develop
git push -u origin feature/new-thing # 3. Push
# ...open PR on GitHub, get review... # 4. PR
# ...merge on GitHub... # 5. Merge
git switch main # 6. Clean up
git pull # Get the merge
git branch -d feature/new-thing # Delete local
git push origin --delete feature/new-thing # Delete remote
Recovering Deleted Branches
If you accidentally deleted a branch, you can recover it using the reflog:
# Find the last commit of the deleted branch
git reflog
# Look for the commit, then recreate the branch
git branch recovered-branch a1b2c3d
Tags vs Branches
While branches are moving pointers (they advance with each commit), tags are fixed pointers to specific commits — perfect for marking releases:
# Create a tag
git tag v1.0.0
git tag -a v2.0.0 -m "Version 2.0 release"
# List tags
git tag
# Push tags to remote
git push origin v1.0.0
git push origin --tags # Push all tags
# Delete a tag
git tag -d v1.0.0
git push origin --delete v1.0.0
Git Crash Course Complete!
Congratulations! You've completed the entire Git Crash Course. You now know how to initialize repos, stage and commit changes, navigate history, undo mistakes, create and manage branches, merge code, and keep your repository clean. These skills will serve you in every software project, whether you're working solo or on a team of hundreds. Keep practicing — Git becomes second nature with daily use.