How Do I Delete a Git Branch Locally and Remotely?

Better Stack Team
Updated on June 21, 2024

To delete a Git branch both locally and remotely, you'll need to follow a couple of steps. Here's how you can do it:

Step 1: Delete the branch locally

First, you need to delete the branch from your local repository. You can do this using the following command:

 
git branch -d branch_name

Replace branch_name with the name of the branch you want to delete. If the branch has unmerged changes, you might need to use -D instead of -d to force the deletion.

Step 2: Delete the branch remotely

To delete the branch from the remote repository (e.g., GitHub, GitLab), you can use the following command:

 
git push origin --delete branch_name

Replace branch_name with the name of the branch you want to delete. This command will delete the branch from the remote repository specified by origin.

Shortcut

You can also combine both steps into one command to delete the branch both locally and remotely in one go:

 
git push origin --delete branch_name && git branch -d branch_name

This command will first delete the branch remotely and then delete it locally. Replace branch_name with the name of the branch you want to delete.

Note:

  • Deleting a branch is a permanent action. Make sure you don't need the branch or its changes before deleting it.
  • Be cautious when deleting branches, especially if they contain important changes. Always double-check before executing the delete command.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.