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.
How Delete a Git Branch Locally and Remotely
Git is a very powerful 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. And, to do this developers create multiple branches so that team collaboration would become easier, and with time, it is important to delete those Git Branches, both Locally and remotely.
Now, as a developer, it is important for you to know how to delete a Git Branch Locally and Remotely as well because it will lead to a better productivity approach, and for that, we have discussed both ways to delete Git Branch. These are very simple git commands, which you can apply and get the desired result. But before that yous should know Why and when to Delete a Git Branches?
Contact Us