Delete Branch

Last updated on

One 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.

Delete a Local Git 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.

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.”.

github list branches

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.

git delete branch

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.

Delete Branch withouth Selecting the Branch

Thus, you must move the cursor to another branch using the select command: git switch [branch-name]. Then, you can directly delete it using the command below.

git branch -d new-file

Or, you can delete it permanently by using the -D flag instead of -d. Thus, it would be written as the command below.

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.

Effects of Branch Deletion on 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.

git reflog

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.

Deleting a Branch on a Remote Repository

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 git push fits into the final command.

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 origin with the remote, as usually provided by default.

delete a branch

Let’s verify the deletion action.

We can use the git push origin :[BRANCH-NAME] command to delete the branch, but we also have to remove it from our local end after deleting the remote reference branch.

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 git branch command to locate the branch you need to remove.
  • Switch Branch: It is better to switch branches before deleting.
  • Delete Branch: Use git branch -d or the --force option (git branch -D) to delete unmerged remote changes.

Deleting a Remote Git Branch:

Here is how to delete a branch in a remote repo using git push alias: git push origin --delete, similarly represented as syncing. Verify the deletion with 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!

Share on: