Deleting a Git Branch Locally
- List All Branches
git branch
- Switch to a Different Branch
git checkout main
- 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
- 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.
- 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