How to Delete a Git Branch Locally and Remotely

Avatar

By squashlabs, Last Updated: August 20, 2023

How to Delete a Git Branch Locally and Remotely

Introduction

Deleting a Git branch, both locally and remotely, is a common task in software development. It is important to clean up branches that are no longer needed to keep the repository organized and to avoid confusion. This guide will walk you through the steps to delete a Git branch locally and remotely, explaining why this task is necessary and providing alternative ideas and best practices along the way.

Related Article: How To Find The Original URL of a Local Git Repository

Deleting a Git Branch Locally

To delete a Git branch locally, you can use the git branch command with the -d flag, followed by the branch name. Here’s how you can do it:

1. Open a terminal or command prompt and navigate to the local Git repository where the branch exists.

2. List all the branches available in the repository using the command:

git branch

This will display a list of branches, with the current branch highlighted.

3. Identify the branch you want to delete from the list. Make sure you are on a different branch before deleting the target branch. You can switch branches using the git checkout command followed by the branch name.

4. Delete the branch using the command:

git branch -d branch_name

Replace branch_name with the name of the branch you want to delete.

5. Verify that the branch has been deleted by listing the branches again:

git branch

The deleted branch should no longer appear in the list.

Deleting a Git Branch Remotely

Deleting a Git branch remotely involves two steps: deleting it locally first and then pushing that change to the remote repository. Here’s how you can do it:

1. Delete the branch locally following the steps mentioned in the previous section.

2. Push the deletion to the remote repository using the command:

git push origin --delete branch_name

Replace branch_name with the name of the branch you want to delete.

3. Verify that the branch has been deleted remotely by checking the remote repository (e.g., GitHub, GitLab, Bitbucket) or by listing the branches using the command:

git branch -r

The deleted branch should no longer appear in the remote branch list.

Alternative Ideas and Best Practices

1. Merge and Delete: Before deleting a branch, make sure it has been merged into the main branch. Deleting an unmerged branch might result in the loss of valuable work. To merge a branch, use the command:

git merge branch_name

2. Protected Branches: Protecting important branches, such as master or main, can prevent accidental deletion. This is particularly useful in collaborative environments where multiple developers work on the same repository. Most Git hosting platforms provide branch protection settings.

3. Branch Naming Conventions: Establishing a clear and consistent branch naming convention can improve the readability and maintainability of your repository. Consider using prefixes or tags to indicate the purpose or type of the branch (e.g., feature/add-login, bugfix/fix-bug-123).

4. Regular Branch Cleanup: Regularly reviewing and deleting branches that are no longer needed helps keep the repository clean and organized. Consider establishing a branch lifecycle policy to determine when and how branches should be deleted.

Related Article: How to Git Pull from a Specific Branch

More Articles from the Git Tutorial: From Basics to Advanced Concepts series:

How to Download a Single Folder from a Github Repo

Downloading a single folder from a GitHub repository using Git can be done in two ways: using the GitHub website or using the Git command-line tool. Additionally, there... read more

How to Login to a Git Remote Repository

Logging in to Git is an essential skill for any software developer. This article provides a step-by-step guide on how to navigate the login process, from installing Git... read more

How to Create a Tag in a GitHub Repository

Creating a tag in a GitHub repository is an essential step in code versioning. This step-by-step guide will walk you through the process, from cloning the repository to... read more

How to Clone a Git Repository Into a Specific Directory

Cloning a Git repository into a specific directory is a fundamental skill for software developers. This step-by-step guide provides two methods for accomplishing this... read more

How to Undo/Revert a Git Commit Before Push

When working with Git, it's important to know how to undo a commit before pushing it to the repository. This article provides a simple guide on removing a Git commit... read more

How to Revert a Pushed Merge Commit in Git

Reverting a pushed merge commit in Git can be a daunting task, but with the right approach, it can be done efficiently. In this article, we provide a step-by-step guide... read more