How do I delete a Git branch locally and remotely?

To delete a Git branch locally, you can use the ‘git branch‘ command with the ‘-d‘ flag and the name of the branch you want to delete. For example:

git branch -d my-branch

This will delete the my-branch a branch from your local repository. If you want to force the delete, even if the branch has unmerged changes, you can use the -D flag instead.

To delete a Git branch remotely, you can use the git push command with the name of the remote repository and the name of the branch you want to delete, preceded by a colon (‘:‘). For example:

git push origin :my-branch

This will delete the ‘my-branch‘ branch from the ‘origin‘ remote repository.

It’s worth noting that you cannot delete the branch you are currently on. To delete the current branch, you first need to switch to a different branch using the ‘git checkout‘ command.

git checkout my-other-branch
git branch -d my-branch
git push origin :my-branch

Deleting a branch will permanently remove the branch and all its commits from your repository. If you want to preserve the commits in your repository, you can merge the branch into another branch instead of deleting it.

Leave a Reply