To delete a Git branch both locally and remotely, follow these steps:
1. Delete a Local Branch
Run the following command to delete a local branch:
git branch -d <branch_name>
- Use
-d
for a safe deletion, which will only delete the branch if it has been fully merged. - Use
-D
if you want to force delete the branch, even if it hasn’t been merged.
Example:
git branch -d my-feature-branch
If the branch hasn't been merged, force delete it with:
git branch -D my-feature-branch
2. Delete a Remote Branch
Run the following command to delete a branch from the remote repository:
git push origin --delete <branch_name>
Example:
git push origin --delete my-feature-branch
This will remove the branch from the remote (e.g., GitHub, GitLab).