Delete Git Branch Locally: Step-by-Step Guide

To delete a Git branch locally, you can use the command “git branch -d <branch-name>“. In certain cases, you might also have to use “-D” to force delete a local branch.

Now, let’s see how to delete a branch locally in Git, why it is important to delete a local branch, and what should be done in case of any errors or force delete a local branch.

git delete branch locally

Importance of Deleting Local Branch in Git

When working with a git project, you will likely accumulate many local branches. Deleting branches you no longer need helps declutter your repository.

Here are some of the reasons for deleting local branches in Git:

  1. Workspace Organization: Removing branches that have completed their purpose keeps the workspace clean and organized.
  2. Clutter Reduction: Deleting redundant branches reduces clutter, making it easier to navigate and work within the repository.
  3. Version Control Streamlining: It ensures that only relevant and active branches are visible, simplifying version control management.
  4. Efficiency Boost: Deleting unnecessary branches promotes an efficient development process by focusing on current tasks and preventing distractions.
  5. Error Prevention: It reduces the likelihood of errors or confusion caused by working on outdated or irrelevant branches.

Check out: 31 Git Interview Questions & Answers

Steps to Delete Git Branch Locally

Prerequisites

Before deleting a git branch, make sure:

  • You are in the correct git repository. Check with git branch to see existing branches.
  • You have checked out to another branch. You cannot delete the branch you are currently on.
  • Commit and Merge your changes in Git before deleting the local branch

If you want to follow the tutorial, you can install git on Ubuntu and try this.

Step 1: Checkout Target Branch

First checkout to the branch you want to keep using or to the main branch. For example, to checkout the main use the below command:

git checkout main

This changes your HEAD to the main branch so you are not on the branch you want to delete.

Step 2: Get Branch List

Next view all local branches and make a note of the one you want to delete:

git branch
or 
git branch -l

Sample output:

$ git branch
  jira-250-feature
* main

Step 3: Delete Local Branch

Now delete the target branch using git branch -d plus the branch name. For example to delete my-feature:

git branch -d jira-250-feature

Sample output:

$ git branch -d jira-250-feature
Deleted branch jira-250-feature (was d8144f7).

If the branch had unmerged changes, you may see an error like:

error: The branch 'jira-250-feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D jira-250-feature'.

To force delete the local branch with changes, use -D instead of -d.

$ git branch -D jira-250-feature

Note: This error can also occur if you have fully merged your branch after creating a pull request on GitHub. So if you are confident that the branch is merged completely, go ahead and use force delete (-D).

Step 4: Confirm Deletion

Finally double check that the local branch no longer appears:

$ git branch 
* main

The branch has been deleted from your local repository. Keep working and deleting old branches to maintain a clean git workflow!

Conclusion

In this article on “Delete Git Branch Locally”, you learned how to view branches, checkout a target branch, and delete local branches that are no longer needed from your local Git repository with the help of just a few commands.

We hope you found this git branch deletion tutorial helpful! Let us know in the comments if you have any questions. Happy coding!

Buy me a coffeeBuy me a coffee

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.