How to Delete a Git Branch Locally and Remotely

Deleting a Git Branch Locally

  1. List All Branches

git branch

  1. Switch to a Different Branch

git checkout main

  1. Delete the Local Branch

git branch -d branch_name

Replace branch_name with the name of the branch you want to delete.

If the branch hasn’t been merged yet, and you’re sure you want to delete it, use the -D (uppercase) option to force deletion:

git branch -D branch_name

Deleting a Git Branch Remotely

  1. Push a Delete Request to the Remote Repository

git push origin --delete branch_name

Replace branch_name with the name of the branch you want to delete.

origin refers to the default remote repository, but if your remote has a different name, replace origin with the correct name.

  1. Verify Remote Branch Deletion

git branch -r

Cleaning Up Stale Remote Branches Locally

To remove references to deleted remote branches, you can run the following command:

git fetch --prune

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top