How to forcefully delete a remote branch in GitHub?

6.7k views Asked by At

I have 2 remote branches :

 - pending-issues-in-project
 - new-issues-in-project

I tried to delete pending-issues-in-project like this :
git push origin :pending-issues-in-project, but i got the error :

error: unable to push to unqualified destination: pending-issues-in-project
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 '[email protected]:forkedRepo/RepoName.git'

So i thought may be i have deleted pending-issues-in-project branch, but when i run
git branch -a it shows pending-issues-in-project branch in the list.
When i tried same (tried deleting) for new-issues-in-project, it worked.
I have already deleted both branches from local server using git branch -D branchName.

If the error is coming because the branch not exist in repository then why its coming in remote branch list?
and
Is there any way to forcefully deletion of the remote branch?

Thanks for your time.

2

There are 2 answers

0
manojlds On BEST ANSWER

You have to do:

git remote prune origin

to remove that remote tracking branch in your local git repository ( prune removes any branch that does not exist in the remote origin anymore). After that, you will not see it under git branch -a

0
mrj On

The branch you're seeing is what's called a remote [tracking] branch. It's in your local repository, and it represents the last place you saw that branch in the remote repository. Git uses branches like this so that you don't have to talk to the remote repository every single time you want to deal with it; you just update/fetch once, the remote racking branch is updated, and you can work from that. In your case, the branch in the remote repository is long since deleted; you just need to remove the copy in your local repository.

There are two main ways to delete it:

  • git branch -d -r origin/pending-issues-in-project removes just that branch; and
  • git remote prune origin deletes all such stale remote branches. You can also update at the same time: git remote update --prune origin