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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github