How can you delete a branch in Git?
Posted by CarolTh
Last Updated: July 12, 2024
Deleting a branch in Git can be done for both local and remote branches. Here’s how to do it in both cases:
Deleting a Local Branch
To delete a local branch, you would use the following command:
git branch -d branch_name
- The -d option stands for "delete". Git will prevent the deletion if the branch has unmerged changes to protect you from losing work. - If you are sure you want to delete the branch regardless of its merge status, use the -D option (uppercase D):
git branch -D branch_name
Deleting a Remote Branch
To delete a branch on a remote repository, you would use the following command:
git push origin --delete branch_name
Alternatively, you can use this syntax:
git push origin :branch_name
Summary
1. Local branch: - Safe deletion: git branch -d branch_name - Force deletion: git branch -D branch_name 2. Remote branch: - git push origin --delete branch_name or git push origin :branch_name Always ensure that you really want to delete a branch, especially a remote branch, as it may affect other collaborators.