Data report"State of code review 2024" is now liveRead the full report

How to delete a branch in Git

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Git, a widely used version control system, offers powerful features such as branching to help developers manage their codebase effectively. However, users may occasionally encounter issues like being unable to delete a branch. This can interfere with Git operations and hinder the process of code management. This guide will provide a comprehensive, step-by-step solution to this common Git error.

  1. Identify the Branch to be Deleted: Before proceeding with the deletion, ensure you have correctly identified the branch you want to remove. Use the git branch command to list all the branches in your repository.
Terminal
git branch
  1. Switch to a Different Branch: You cannot delete a branch while you are on it. To switch to another branch, use the git checkout command followed by the name of the branch.
Terminal
git checkout <another-branch>
  1. Delete the Local Branch: Git provides two options for deleting a branch: -d and -D. The -d option deletes a branch only if it has been merged into another branch. If it hasn't been merged, use the -D option, which forces the deletion.
Terminal
git branch -d <branch-name>

or

Terminal
git branch -D <branch-name>
  1. Delete the Remote Branch: If the branch is also present on the remote repository, use the git push command with the --delete option to remove it.
Terminal
git push origin --delete <branch-name>

If you encounter issues while deleting a branch, consider the following solutions:

  • Ensure you are not currently on the branch you are trying to delete. Git won't allow the deletion of a currently checked-out branch.

  • If the -d option fails to delete a branch, it's probably because the branch hasn't been merged. You can force the deletion using the -D option, but be aware that you will lose all changes on the branch that aren't merged.

  • Remember to delete the branch from the remote repository if it exists there. Otherwise, it might be restored the next time you pull from the remote.

Q: What is the difference between git branch -d and git branch -D?

The -d option deletes a branch only if it has been merged into another branch. If it hasn't been merged, the -D option forces the deletion, but you will lose all changes on the branch that aren't merged.

Q: Why can't I delete a branch in Git?

You might be trying to delete a branch while you are on it, which Git does not allow. Also, if the branch hasn't been merged, you need to use the -D option to force the deletion.

Q: Can I recover a deleted branch in Git?

Yes, if you accidentally delete a branch, you can recover it using the git reflog and git branch commands.

Deleting a branch in Git is a straightforward process once you understand the steps involved. Remember to always switch to a different branch before attempting to delete one, and use the correct options (-d or -D) based on whether your branch has been merged.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2