Delete Branch
Last updated onOne of the key things in using Git is to manage your branches well, which means you will need to use the Git delete branch command for those that are no longer needed. As things progress, and feature after feature gets developed and merged, a mess is bound to happen.
Cleaning up unused branches helps to keep your repository clean and pushes you towards an organized workflow, where it will be easier for you or the rest of your team members to navigate through the project. This tutorial will teach you how to delete both your local and remote Git branches in a safe and efficient manner.
So let’s get started with why we should delete a branch from our repository.
Reasons for Git Branch Deletion
A Git branch is usually deleted once its work has been completed, when a feature has been merged into production or an experiment can no longer be continued.
Additionally, old branches can clutter and complicate your repository structure; this is especially true for large projects with many contributors.
- Merge Configured Changes: Once a branch is merged into the mainline, we should remove the merged branch, as keeping it may clutter the repository.
- Finished Features or Fixes: When there are no more changes to a branch after development, it is a good idea to delete it.
- No Merge Conflicts: When branches are accidentally worked on or merged later, they potentially can lead to merge conflicts.
- Deleting Unused Work Branches: If a branch signifies work that is no longer required or has been abandoned, removing it ends the confusion.
Having a clean Git repository helps keep things tidy and easier to browse through and manage.
A cleaned-up repository is a lot easier to manage than one in which you must sift through dozens and dozens of open PRs; those are ones that might still be ongoing until they get merged.
Long-term, better collaboration is achieved by having a nice and clean branch structure for everyone to be on the same page.
It is also easier to track the progress of an effective project management system with a well-maintained repository containing only essential branches. By removing stale branches, you will keep your Git workflow light and tidy.
In the following section, you will understand how to delete it via git command, let’s move forward.
Git Delete Local Branch
Removing a local Git branch is an easy and basic operation we can do to maintain our repository clean. We will go over the steps as illustrated in this manual and address some of the most frequently encountered problems. Here are steps:
List Your Branches:
Always list all branches to verify that you are targeting the correct branch. Use the following command:
git branch
It will show you all the branches in your local repository, with an asterisk showing the current branch.
Switch to Another Branch:
You can’t remove the branch you are currently in. Before deleting a branch, you need to check out a different branch. For example, switch to the main branch:
git checkout main
Delete the Branch:
After double-checking that you are on a different branch from the target, use this command to remove it:
git branch -d branch-name
Replace branch-name
with a new name from your target branch.
Force Delete if Necessary:
Deletion of the non-merged branch with a force can be done by:
git branch -D branch-name
This action will delete the branch no matter what.
Here is a comparison table:
Action | Command |
Delete a local branch | git branch -d branch-name |
Force delete a branch | git branch -D branch-name |
Delete a remote branch | git push origin --delete branch-name |
So, Let’s see an example:
Delete Git Branch Example
The following image depicts a remote repository containing three branches. I have already cloned all the branches except the one under the name “development.”.
Thus, I will delete the “new-file” branch from the local repository. But before doing that, I will show you a figure to help you understand all the branches along with their commit references.
Let’s see how to delete the new-file
branch from the local repository.
Actually, the git system will not allow you to delete the current branch. So, you have to ensure that you are on another branch (cursor). Otherwise, it will display the following error.
Thus, you must move the cursor to another branch using the select command:
. Then, you can directly delete it using the command below.git switch [branch-name]
git branch -d new-file
Or, you can delete it permanently by using the
flag instead of -D
. Thus, it would be written as the command below.-d
git branch -D new-file
However, if you examine the above figure, you will notice two committed refs there, which are e7f1da9 and 1e2439a. The question then arises: does the deletion have any effect on these? Let’s explore the answer in the following section.
What happened when using Git to remove a branch from its logs?
Actually, that doesn’t affect the commits and structure; it only removes the label name of the branch. However, if those commits have not already been merged into another branch, then deleting the branch will remove all of its commits and references to free up space.
I deleted the ‘new-file
’ branch, but the two refs are still there, allowing me to recreate the branch with the same name.
Here is an image showing you that.
Note: The branch deletion only affects the local repository. But what about the remote repository? Let’s move on to the next part to see how to apply this change remotely.
Git Delete Remote Branch
Deleting a branch in a remote repository is closely related to local branch deletion. Sometimes, you need to remove a branch that is not needed after merging or when you want to clean up the remote repository.
Here’s how to delete a remote branch and how
fits into the final command.git push
git push origin :[BRANCH-NAME]
Or you can use another command
git push origin --delete [BRANCH-NAME]
Replace [
BRANCH-NAME]
with the name of your realm, master, or staging branch; and
with the remote, as usually provided by default.origin
Let’s verify the deletion action.
We can use the
command to delete the branch, but we also have to remove it from our local end after deleting the remote reference branch.git push origin :[BRANCH-NAME]
git branch -r
The list should no longer include the deleted branch.
Let’s summarize it.
Wrapping Up
You can end up accumulating an untidy repository because of all those Git branches used during the course of your project. Keeping a sane workflow by deleting unnecessary branches is also paramount. In this guide, we describe how by removing local and remote Git branches, you can still manage to keep the names of your repository folders.
Why Git Branch Deletion?
- Merged changes: Branches can clutter the repo once they are merged into other branches.
- Finished Features/Fixes: Once you know that no other changes are needed (meaning the branch has been merged into develop or master), deleting it will also leave no room for confusion.
- Avoid Merge Conflicts: Removing branches helps to keep broken code from coming back.
- Cleaning Up Discarded Work: Avoid having all those unneeded branches lying around, which makes it hard to navigate the repository.
Deleting a Local Git Branch:
- Verify Branch: Use
command to locate the branch you need to remove.git branch
- Switch Branch: It is better to switch branches before deleting.
- Delete Branch: Use
or thegit branch -d
option (--force
) to delete unmerged remote changes.git branch -D
Deleting a Remote Git Branch:
Here is how to delete a branch in a remote repo using
alias: git push
, similarly represented as syncing. Verify the deletion with git push origin --delete
.git branch -r
Now you know ways to manage your project repository better and cascade the cleanliness into a collaboration-friendly environment where it can blossom without breathing in constant dust!
Thank you for reading. Happy Coding!
Frequently Asked Questions (FAQs)
How do I delete a local Git branch?
How do I delete a remote Git branch?
Can I delete the current Git branch I'm working on?
What happens if I delete a branch that hasn’t been merged?
Does deleting a Git branch affect the commit history?
How can I list all Git branches before deleting one?
How do I force-delete a local branch in Git?
How do I check if a Git branch was deleted successfully?
How do I delete multiple local branches at once?
How do I recover a deleted branch in Git?