How to delete both a remote and local Git branch |Video upload date:  · Duration: PT8M14S  · Language: EN

Quick guide to delete a Git branch locally and on remote with safe commands and verification steps for clean repository history

Quick plan to avoid accidental regrets

Yes you can delete branches. No you should not do it while on the branch unless you enjoy confusing error messages. This guide walks through a safe, clean way to remove a local branch and its counterpart on the remote repository so your teammates stop asking what that weird feature branch was about.

Step 1 Move off the branch you want to delete

Switch to a stable branch before you try to remove the target. Deleting a branch you are currently on will either fail or make Git sulk loudly.

git switch main
# or
git checkout main

Step 2 Delete the local branch the polite way or the rude way

Pick your temperament. The polite delete refuses to remove a branch that still has unmerged work. The rude delete forces removal whether Git likes it or not.

  • Safe delete which checks merge status
  • git branch -d branch-name
  • Force delete when you are absolutely sure history can go to the void
  • git branch -D branch-name

Pro tip for the cautious

If you want to double check that the branch is merged into your mainline run

git branch --merged

Only delete if your branch shows up in that list unless you have a very good reason.

Step 3 Delete the branch on the remote so your repo stops lying to you

Pushing a delete request removes the branch reference on the remote like origin or github. This is the part that stops the branch from showing up for other developers.

git push origin --delete branch-name

Step 4 Verify and clean up stale references

Fetch with pruning to remove stale remote references from your local clone. Then inspect all refs to confirm the branch is gone.

git fetch --prune
git branch -a

When things still look like a mess

  • If the remote still shows the branch check your remote web interface like GitHub to see if it was protected or blocked
  • Use git fetch --prune again if local metadata is stale

A few best practices so you do not become the person who deletes main

  • Enable branch protection on important branches in your remote hosting service
  • Create a short pre push checklist in your team workflow to prevent accidental deletes
  • Use descriptive branch names so deleting the wrong one causes less drama
  • Prefer safe deletes and only use force when you have confirmed the history can be discarded

Recap

Move off the branch then delete locally with git branch -d or -D if you are forcing it. Push the delete with git push origin --delete branch-name and finish with git fetch --prune and git branch -a to verify. Be careful and set up protections so future you does not get blamed for chaos.

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.