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.
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.
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
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.
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.
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
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 nowgit fetch --prune
to fetch and prune in one goWant to double check before you celebrate The Vanishing Branch Use this to list local and remote branches with details.
git branch -av
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.