Updated on: 7/20/2022
At its core, the branching model offered by Git is intended to help you avoid merging unstable code into the main codebase. Most Git workflows and Git branching strategies require developers to delete Git branches at some point in the development process; often after a branch has been merged into the main branch.
Deleting Git branches is generally considered to be good repository hygiene because it speeds up your repo performance and ensures that only the most current and necessary information is contained in your project.
In this article, we’ll cover how to Git delete local branches using the CLI and GitKraken Client.
- View Your Git Branches Using the CLI & GitKraken Client
- Git Delete Local Branch Using the CLI
- Git Delete Local Branch Using GitKraken Client
- Git Delete Local Branch FAQs
View Your Git Branches Using the CLI & GitKraken Client
Before you can delete a local Git branch, you’ll need to get the exact name of the branch you want to delete.
To access a comprehensive list of local Git branches in your project via the CLI, type git branch
and hit Enter. The terminal will return a list of all the local branches in your Git repo.
We used GitKraken Client in this example, but it’s important to note that If you’re using a terminal other than GitKraken Client, you’ll only be able to see the information contained in the terminal itself.
With GitKraken Client, you can combine the speed and efficiency of the CLI with the convenient visuals offered in a GUI. Meaning you can leverage the built-in terminal as well as easily visualize your Git branches by simply glancing at the Client’s incredibly powerful commit graph. Level up your workflow and try GitKraken Client for free!
Learn more about viewing your Git branch list, including how to view your local and remote Git branches from the CLI & GitKraken Client.
Once you’ve identified the exact name of the Git branch you want to delete, you can move on to actually deleting the branch.
Git Delete Local Branch Using the CLI
To delete a local Git branch using the terminal, run the following:
git branch -d <branch name>
.
Keep in mind, if you’re using a terminal other than GitKraken Client, you won’t have immediate visual confirmation that the Git branch has been properly deleted from your repository. Instead, you’ll have to run git branch
again to view your Git branch list and ensure that the intended Git branch has been deleted.
Git Delete Local Branch Using GitKraken Client
As we’ve already mentioned, there are significant benefits associated with using GitKraken Client to visualize your repository.
If you want to Git delete a local branch using GitKraken Client, simply right-click on a branch name in the left panel or from the central graph and select Delete <branch name>.
Git delete a local branch by mistake? Never fear. You can always utilize the magical Undo button in the top toolbar in GitKraken Client to make it like the mistake never happened.
Want to make deleting local branches in Git quick and easy? Download GitKraken Client for free.
Once you’re comfortable deleting local Git branches, continue to improve your Git skills by learning how to delete a REMOTE Git branch using the CLI & GitKraken Client.
Git Delete Local Branch FAQ
Q: Why Can’t I Delete a Local Git Branch?
A: Git doesn’t allow you to delete a local branch if you have the branch checked out. If you’re having trouble deleting a Git branch, make sure you Git checkout a branch other than the one you want to delete.
Q: How Can I Git Delete a Local Branch with Unmerged Changes?
A: To delete a local Git branch with unmerged changes, you will need to run:
git branch -D <branch name>
This tells Git that you’re serious about deleting this branch. But be warned! Using the -D
flag can often make losing data very easy, so use with caution.