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.

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:

ActionCommand
Delete a local branchgit branch -d branch-name
Force delete a branchgit branch -D branch-name
Delete a remote branchgit 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.”.

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.

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.

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.

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 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!

Frequently Asked Questions (FAQs)

  • How do I delete a local Git branch?

    To delete a local Git branch, you need to first switch to a different branch and then delete the target branch. Here's the command to delete a local branch:
    git branch -d <branch_name>
    If the branch hasn’t been merged, and you want to force-delete it, use:
    git branch -D <branch_name>
  • How do I delete a remote Git branch?

    To delete a branch from a remote repository, you can use the following command:
    git push origin --delete <branch_name>
    Alternatively, you can use:
    git push origin :<branch_name>
    This will remove the branch from the remote repository.
  • Can I delete the current Git branch I'm working on?

    No, Git won’t allow you to delete the branch you’re currently on. You need to switch to a different branch before deleting the target branch. To switch branches, use:
    git checkout <another_branch>
    Then you can proceed with deleting the original branch.
  • What happens if I delete a branch that hasn’t been merged?

    If you delete a branch that hasn’t been merged, the commits associated with that branch might be lost unless they are referenced elsewhere in the repository. You can force-delete an unmerged branch with:
    git branch -D <branch_name>
    This permanently deletes the branch.
  • Does deleting a Git branch affect the commit history?

    Deleting a branch only removes the branch reference; the commits remain in the Git history. If the branch was merged, those commits are still part of the mainline. If not, the commits will be lost unless referenced elsewhere (like in another branch or tag).
  • How can I list all Git branches before deleting one?

    To view all the local branches in your repository, use:
    git branch
    If you want to see remote branches, use:
    git branch -r
    This helps you verify which branch to delete.
  • How do I force-delete a local branch in Git?

    If the branch hasn't been merged or you want to delete it despite potential conflicts, you can force-delete the branch using:
    git branch -D <branch_name>
  • How do I check if a Git branch was deleted successfully?

    After deleting a branch, you can verify it by listing the branches again:
    git branch -r
    (for remote)
    git branch
    (for local) The deleted branch should no longer appear in the list.
  • How do I delete multiple local branches at once?

    You can use a loop in Bash to delete multiple local branches at once:
    git branch | grep <pattern> | xargs git branch -d
    Replace <pattern> with a keyword to match the branches you want to delete.
  • How do I recover a deleted branch in Git?

    If the branch has been deleted, you can recover it using the commit reference with the following command:
    git checkout -b <branch_name> <commit_hash>
    Replace <commit_hash> with the specific commit ID where the branch was before deletion.
Share on: