Back

How to Delete a Local Git Branch

How to Delete a Local Git Branch

Keeping only the currently active branches is critical to maintaining a clean and organized repository. Thus, every developer should know how to remove local branches. This article shows the command to delete a local Git branch and the visual steps to do the same.

Before you can delete a local Git branch, you must ensure it has no uncommitted changes. To verify that, run the command below:

git status

If there are uncommitted changes, you can either commit them with:

git add .
git commit -m "<your-commit-message>"

Or, alternatively, discard them all with:

git restore .

Then, move to a different branch:

git checkout <different-branch-name>

If your repository only has a single branch and you want to create a new one and switch to it, use the -b option as follows:

git checkout -b <new-branch-name>

Great! You are now ready to remove your local branch.

How To Remove a Local Git Branch

Follow the procedures below and see how to remove a local branch using the command line or Git GUI.

Using the CLI

Launch the following command to delete a local branch:

git branch -d <branch-name>

Replace <branch-name> with the name of the branch you want to delete in your local repository.

Note: -d is an abbreviation for the --delete option.

To delete a branch that is not fully merged, you need to use the -D argument, a shortcut for --delete --force:

git branch -D <branch-name>

For example, follow the prerequisite instructions described earlier and run the command to delete the test branch:

git branch -d test

The output will be something like:

Deleted branch test (was 5dc0503).

Using Git GUI

These are steps the steps to delete a local Git branch in the official GUI client in Git for Windows:

  1. Open Git GUI and load your repository.
  2. In the “Branch” menu, select the “Delete…” option.

  1. Select the branch to delete and press the “Delete” button.

Behind the scenes, Git GUI executes the necessary commands to delete the local branch for you.

FAQs

Why do you need to delete a local git branch?

Deleting a local Git branch is essential to keep the repository clean and reduce clutter. It also minimizes the risk of working on branches that are no longer relevant.

How do you remove local branches that no longer exist in the remote repository?

Run the command below to fetch updates from the remote repository and remove any branches that no longer exist:

git fetch --prune

How do you verify that a local git branch has been removed?

List all local branches with the following command, and verify that the branch you deleted is not in the list:

git branch

How do you deal with the “Cannot delete branch … checked out at …’” error?

That error occurs if you are currently on the branch you are trying to delete or there are uncommitted local changes. To address it, refer to the first chapter of the guide.

Conclusion

By following the alternative procedures described in this article, you can delete a local Hit branch in the command line or visually with the Git GUI client.

Gain control over your UX

See how users are using your site as if you were sitting next to them, learn and iterate faster with OpenReplay. — the open-source session replay tool for developers. Self-host it in minutes, and have complete control over your customer data. Check our GitHub repo and join the thousands of developers in our community.

OpenReplay