Delete a Git Branch Locally & Remotely |Video upload date:  · Duration: PT1M0S  · Language: EN

Quick guide to delete a Git branch locally and on the remote with safe and force commands and cleanup tips

So you built a feature branch, fixed a bug, and now you want to get rid of the evidence. Good. This guide walks through removing a Git branch on your machine and telling the remote to forget it too. It keeps the facts straight and the drama low.

Before you delete a branch

Deleting branches is easy and permanent enough to be fun if you enjoy surprises. Do these checks first so the surprise is a pleasant one.

  • Confirm you are not on the branch you want to delete
  • Make sure any important work is merged or backed up
  • Check branch protection rules on your remote hosting service

Switch away from the target branch

Git will not let you delete the branch you are currently using. So switch to a safe place first like main or develop. Pick whatever branch your team actually uses for shipping.

git switch main

Delete the local branch safely

Use the safe delete command when you want Git to protect you from losing commits. This command refuses to delete branches that contain commits not merged anywhere else.

git branch -d feature-name

If Git says the branch is not fully merged then you either need to merge it or be certain you want to lose the commits.

Force delete when you know what you are doing

Okay bold move. If you are absolutely sure the branch is expendable use the force delete. This nukes the branch locally even if commits would be orphaned.

git branch -D feature-name

Use this with care and maybe a small prayer to the version control gods.

Remove the branch on the remote

Tell the remote to drop the branch. This is just a push that deletes a ref on the server. If it fails inspect permissions and branch protection rules on your hosting service.

git push origin --delete feature-name

Prune stale remote references

After deleting a remote branch your local clone may still list it until you clean up stale refs. Run one of these to keep your branch list honest.

  • git remote prune origin to remove stale refs now
  • git fetch --prune to fetch and prune in one go

Verify what remains

Want to double check before you celebrate The Vanishing Branch Use this to list local and remote branches with details.

git branch -av

Quick safety tips

  • Create a lightweight backup tag if you want to be extra cautious
git tag backup/feature feature-name

One final thought Keep a calm checklist and confirm branch names before running destructive commands. That small pause saves you from a frantic revert later and maybe from explaining the mistake in a team meeting.

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.