Tools2023-03-07

How to Delete a Branch in Git: Local and Remote

Learn how to delete branches locally and remotely in Git using CLI and efficiently handle separate lines of development within a repository.
Deleting Remote & Local Branches Like a Pro in Git

Git is a powerful and widely-used version control system that allows developers to track changes in their codebase and collaborate on projects with ease. One of the key features of Git is its ability to create and manage branches, which allows developers to work on different versions of the codebase simultaneously. In this article, we will discuss how to delete branch in Git like a pro using CLI, and other best practices for branch management.

What are branches in Git?

A Git branch is a separate line of development within a repository. It allows multiple developers to work on different versions of the codebase simultaneously without affecting the main branch. When a branch is no longer needed, it can be deleted to keep the repository clean and organized. Git branches are implemented in a lightweight manner, unlike other version control systems (VCS). Instead of copying files between directories, a branch in Git is simply a reference to a commit. This means that a branch serves as a pointer to the latest commit in a series of commits.

The git branch command is a powerful tool that enables developers to manage branches in a Git repository. It allows developers to create new branches, list existing branches, rename existing branches, and delete branches that are no longer needed. While git branch is a useful command, it is not capable of switching between branches or merging a forked history. To perform these tasks, git checkout and git merge commands can be used.

$ git branch <branch-name>

The above command in CLI creates a new branch called <branch-name>. Remember that the new branch is not checked out with this command.

$ git branch

This lists all the branches. Adding -a  to this will list all remote branches.

When to Delete Your Git Branches?

There are several cases when one needs to delete branches in Git: 

  • The branch is no longer needed: This could be because the feature or bug fix that the branch was created for has been completed and merged into the main branch. 
  • The branch is outdated: If the branch was created a long time ago and is no longer relevant to the current development, it may be best to delete it to avoid confusion.
  • The branch is causing conflicts: If a branch is causing conflicts with other branches or the main branch, it may be best to delete it to resolve the conflicts.

How to Delete Branches in Git?

Now that we understand how branches in Git works, let's see how to delete local git branches:

1. Deleting Git Branches Locally

Deleting a branch in Git does not erase the commits themselves. In fact, the commits still exist and can potentially be recovered. The outcome of deleting a branch is contingent on whether the branch was previously merged or not. When a branch has been fully merged, it is considered "safe" to delete. This means that all of the commits on the branch have been incorporated into the main branch and are no longer needed.

In this case, you can use the command to delete the branch:

$ git branch -d branch-name 

On the other hand, if a branch has not been fully merged, you should be cautious when deleting it. This is because the commits on the branch may contain changes that have not been backed up and can be permanently lost if the branch is deleted. In this case, you can use the command:

$ git branch -D branch_name 

to forcefully delete the branch, but be aware that this action can permanently delete commits and changes that have not been backed up. Still, if you act quickly you can recover them. That will be discussed later. It is important to note that you can use:

 $ git branch -vv 

to see the branches that are merged or not and you can use $ git log to see all the commits made in the branch before deletion.

2. Deleting All Local Branches

You can use the git command along with grep to delete multiple branches that match a certain pattern. This can be useful when you need to delete a group of branches that share a similar name or follow a certain naming convention. For instance, the following command uses grep and xargs to delete all branches that have been merged except the current branch:

$ git branch -D $(git branch --merged | grep -v \* | xargs)

3. Deleting Git Branches Remotely

The branch delete commands mentioned yet are only for the local git repository. Now let’s see how to delete git branches remotely. To delete a remote branch in Git, you can use the command:

git push origin --delete branch_name

This command will delete the specified branch from the remote repository, but it will not delete the local copy of the branch.

$ git push <remote-name> --delete <branch-name>

How to Undelete Git Branches?

Deleting a branch in Git is a permanent action, but there are ways to recover deleted branches if you need to. One way to do this is by using the git reflog command. The git reflog command allows you to view a log of all the actions that have been performed on your repository, including branch deletions.

By using this command, you can find the SHA-1 hash of the commit that was the tip of the deleted branch. Once you have the SHA-1 hash, you can create a new branch that points to that commit.

The command:

$ git branch <branch-name> <commit-id> 

will create a new branch called "feature_branch" that points to the commit with the SHA-1 hash <commit-id>. This effectively undoes the deletion of the branch.

Conclusion

Deleting branches in Git can be a useful tool for keeping your repository organized and free of unnecessary clutter. By understanding the different methods for deleting branches, following best practices for safe deletion, and communicating with your team, you can even become a Git ninja. Remember to always be cautious when deleting branches, and to back up any important changes before doing so.

To get maximum efficiency out of Git, an engineering analytics tool is the need of the hour. Hatica offers metrics across 13 dashboards, powered by CI/CD tools, Jira, Asana and GitHub. By collating tool activities at one place, Hatica helps teams streamline their workflow, cut through the clutter of unwanted alerts, and improve productivity. Request a demo with Hatica today!

FAQs

1. What if the local branch hasn't been merged yet?

If the local branch you want to delete hasn't been merged into its parent branch, you can use git branch -D branch_name instead of -d. The -D flag forces the deletion regardless of merge status.

2. What happens to the commits in a deleted branch?

Merged branches retain their commits. Otherwise, access to commits may be easier with hashes or recovery methods.

3. Are there any risks associated with deleting branches?

Yes, there are risks. If you delete a branch without merging its changes, those changes may be lost. Additionally, if you delete a branch in a shared repository, other team members who depend on that branch may be affected.

Subscribe to Hatica's blog

Get bi-weekly insights straight to your inbox

Share this article:
Table of Contents
  • What are branches in Git?
  • When to Delete Your Git Branches?
  • How to Delete Branches in Git?
  • 1. Deleting Git Branches Locally
  • 2. Deleting All Local Branches
  • 3. Deleting Git Branches Remotely
  • How to Undelete Git Branches?
  • Conclusion
  • FAQs
  • 1. What if the local branch hasn't been merged yet?
  • 2. What happens to the commits in a deleted branch?
  • 3. Are there any risks associated with deleting branches?

Ready to dive in? Start your free trial today

Overview dashboard from Hatica