How Delete a Git Branch Locally and Remotely?
Last Updated :
02 Jul, 2024
In Git, branches are an important part of the workflow, allowing developers to work on different features, bug fixes etc. without affecting the main codebase. However, once a branch has served its purpose, it is a good practice to delete it to keep the repository clean and organized. This article will guide you through the steps to delete a Git branch both locally and remotely.
Delete a Git Branch Locally
Git won’t allow you to delete a Git branch you are currently working on. So you must make sure to checkout to a branch that you are NOT deleting. For this use the command:
Syntax
git checkout <branch-name>
Here we will check out our main branch from my test branch.

Now in order to delete the test branch locally, we use the command :
Syntax
git branch -d <branch-name>
We will delete my test branch as an example.

Note: The -d option will delete the branch only if it has already been pushed and merged with the remote branch. If you want to forcefully delete a branch you will have to use the -D option instead. The -D flag is synonymous with –delete –force. This will forcefully delete the branch even if it hasn’t been pushed or merged with the remote. the full command is:
Syntax
git branch -D <branch-name>
With this, we can successfully delete a local branch.
Delete a Git Branch Remotely
You can’t use the git branch command to delete a remote branch. Instead, you have to use the git push command with the –delete flag, followed by the name of the branch that you want to delete. You also need to specify the remote name (origin in this case) after “git push”. The command is as follows:
Syntax
git push <remote-name> --delete <branch-name>
Here I will delete my test branch in my remote repository as shown below.

This command will delete the branch remotely. You can also use the shorthand:
Syntax
git push <remote-name> :<branch-name>
As you can see my remote branch is no more in my GitHub repo:

With this, we have successfully deleted our remote branch. A common error faced by many in this step is:
error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to ‘git@repository_name’
This means that someone has already deleted the branch that you want to delete. If this happens you can use the following command to synchronize your branch list in the local environment:
Syntax
git fetch -p
The -p flag here means “prune”. After fetching the branches which no longer exist remotely will be deleted in your local working environment.
Local Deletion vs Remote Deletion
Action |
Local Deletion |
Remote Deletion |
Command |
git branch -d branch-name |
git push origin --delete branch-name |
Force Deletion |
git branch -D branch-name |
git push origin :branch-name |
Purpose |
Removes branch from local repository |
Removes branch from remote repository |
Safety |
-d flag is safer, prevents unintentional deletion |
Must ensure the branch is not needed anymore |
Conclusion
Removing the branches which are not important keeps the repository organized, improves collaboration, and makes sure a better development workflow. However, it’s important to exercise caution when deleting branches, especially remote branches, as it may impact ongoing work by other team members. Therefore, always communicate with your team and verify that the branch is no longer required before proceeding with deletion.
Similar Reads
How to Delete Local Branch in Git?
Git is a widely used distributed version control and source code management system. It effectively tracks changes to source code, enabling effortless branching, merging, and versioning. What are Local Branches? In Git, branches are lightweight pointers to commits in the repository's history. Each br
1 min read
How to Delete All Local Branches in Git?
Git provides a powerful version control system that allows developers to collaborate efficiently on projects. Over time, however, a repository can have a lot of local branches that are no longer needed, cluttering the workspace and potentially causing confusion. The "main" is the by default branch c
3 min read
How To Delete Remote Branch in Git?
Git is an important tool in the process of application development and is used widely in the software industry by developers to maintain the codebase. Using this developers are able to organize their codebase and manage the version history of their project. Now, as a developer, you need to know how
1 min read
How to Delete a Branch in Git?
When working with Git, itâs common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, itâs often necessary to delete it to keep your repository clean and organized. In this article, weâll see the process of deleting a Git branch us
3 min read
How to Create a Remote Git Branch?
Creating a remote Git branch allows multiple developers to work on different features or fixes simultaneously. It ensures a clean and organized workflow, enabling collaboration without affecting the main codebase. This article covers the steps to create a remote Git branch and push it to a remote re
2 min read
How To Clean Old Remote Git Branches?
Over the time, remote Git repositories can have outdated or unused branches that clutter the project history and make it harder to navigate. Cleaning up these branches is important for maintaining a well-organized repository. In this article, we will learn how to clean old remote Git branches. Table
3 min read
How to Compare a Local Git Branch with its Remote Branch ?
When working with Git, it's often necessary to compare your local branch with its remote counterpart to understand the differences in terms of commits and changes. This helps in keeping your local work in sync with the remote repository and managing potential conflicts. This article will guide you t
3 min read
How to Delete Branch in Github?
In Git repositories, branches are important for organizing and managing code changes, but over time, unused or outdated branches can clutter the repository. Deleting these branches helps maintain a clean and manageable codebase. In this article, we'll explore the process of deleting branches in GitH
2 min read
How to Delete Branch in Gitlab?
When working with Git, it's common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, it's often necessary to delete it to keep your repository clean and organized. In this article, we'll see the process of deleting a Git branch us
2 min read
How to Create a New Branch in Git and Push the Code?
Branching in Git is a helpful feature for software developers working on a big team project. It allows the team members to work on different aspects of the software by creating a branch from the main branch. The main branch is not affected by the changes in the created branch until it is merged into
8 min read