A Git Branch Remove Example |Video upload date:  · Duration: PT8M6S  · Language: EN

Learn how to safely delete local and remote Git branches with clear commands and precautions to avoid losing work

If your repository looks like a hedgehog made of feature branches you forgot about, you are not alone. This guide shows how to inspect, remove, and clean up git branches without accidentally tossing work into the void. It stays accurate while being a little sarcastic about our collective tendency to never delete anything.

Look before you leap

Start by checking what actually exists. A quick inventory prevents deleting the branch that paid your rent last month.

git branch --all
git status

These commands list local and remote refs and show your current branch. If you are sitting on the branch you mean to delete, switch off it first.

Switch to a safe branch

Do not try to remove the branch you are standing on unless you enjoy confusing errors.

git switch main
# or for older git versions
git checkout main

Delete a local branch the safe way

If the branch has been merged, use the delete flag that refuses to delete unmerged work. Think of it as the adult supervision option.

git branch -d feature-branch

This command will fail if the branch has commits that are not merged into your current branch. That gives you a chance to recover or rethink that experiment with the mysterious README.

Force delete when you mean it

If the branch is garbage or you are certain the changes are not needed, force removal removes the branch and its pointer. This is irreversible for most users without digging into reflog.

git branch -D feature-branch

Use this only when you have confirmed nothing important is lost. Yes that includes the clever hack that barely worked in testing.

Remove a remote branch on GitHub or other hosts

To ask the remote to drop a branch, push a delete request. Hosting providers may block this with branch protection, so if it fails check the repo settings or contact an admin.

git push origin --delete feature-branch

Prune local references to removed remote branches

After the remote branch is gone the local list may still show it. Clean up stale refs with one of these commands.

git fetch --prune
# or
git remote prune origin

That makes your branch list honest again. Good for your repo and for your reputation during demos.

Verify the cleanup

Do a final check to ensure the branch is nowhere to be found locally or on the remote.

git branch -a

See nothing? Mission accomplished. See the branch still listed remotely? Revisit the remote deletion step or check for branch protection on GitHub.

Quick checklist for sane branch cleanup

  • Inspect branches with git branch --all and git status
  • Switch off the branch before deleting
  • Use git branch -d for safe deletion
  • Use git branch -D only when you want to force removal
  • Remove remote branch with git push origin --delete branch-name
  • Prune stale refs with git fetch --prune or git remote prune origin
  • Verify with git branch -a

That is all there is to a tidy branch cleanup workflow. Follow these steps and your repo will stop looking like an abandoned garage sale. If something goes wrong you can usually recover from reflog but it is far less fun than taking two extra seconds to double check which branch you are about to delete.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.