Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot delete a remote branch created unintentionally

$ git branch -a * SocialAct   master   remotes/origin/HEAD -> origin/master   remotes/origin/SocialAct   remotes/origin/social 

I want to delete the remote branch "remotes/origin/social", and applied folloing command:

$ git branch -d -r origin/social Deleted remote branch origin/social (was 26f6f61). 

But I have no idea how to bring these changes remotely so that the branches are deleted from origin and everyone can see the changes. I tried git push but that does not work

Any help.

like image 966
Himel Avatar asked May 24 '10 09:05

Himel


People also ask

How do I force delete a remote branch?

Deleting remote branches To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name ( origin in this case) after git push .

Why can't I delete my git branch?

A: Git doesn't allow you to delete a local branch if you have the branch checked out. If you're having trouble deleting a Git branch, make sure you Git checkout a branch other than the one you want to delete.

How do I delete a branch I just made?

Delete local Git branch command The command to delete a local git branch can take one of two forms: git branch –delete old-branch. git branch -d old-branch.

Can I delete a remote branch?

Instead of using the git branch command that you use for local branches, you can delete a remote branche with the git push command. Then you specify the name of the remote, which in most cases is origin . -d is the flag for deleting, an alias for --delete . remote_branch_name is the remote branch you want to delete.


2 Answers

I had this error (from above):

Thanks. Actually I noticed this solution and tried earlier. But this gives following error... $ git push origin :heads/socail Enter passphrase for key '/h/.ssh/id_rsa': error: unable to push to unqualified destination: heads/socail 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]' – Himel May 24 '10 at 9:37

It seemed to have gotten confused about whether or not I had actually deleted it remotely. I worked around it like so:

git push origin HEAD:branch_to_delete  git push origin :branch_to_delete 

That worked for me. Using: git version 1.7.3.1.msysgit.0.

like image 63
Nathan McDaniel Avatar answered Sep 24 '22 14:09

Nathan McDaniel


As mentioned by @Josh in a comment on Nathan McDaniel's Answer, this is likely due to the branch no longer existing in the remote repository. This causes git branch -a to still show the branch under origin (or whatever you happened to name this particular remote), but deleting the branch on the remote repository is impossible because it no longer exists on the remote. This could have been caused by deleting the branch on the remote from another computer (on top of the fact that git pull and git fetch do not remove references to remote branches that have been deleted from the remote repository).

The fix

Simply remove all remote-tracking branches that have already been removed from the remote repository with git remote prune:

git remote prune REMOTENAME 

For example, if your remote's name is origin (likely), the above command would look like:

git remote prune origin 

From the documentation provided with git:

git remote prune [-n | --dry-run] <name>

Deletes all stale remote-tracking branches under <name>. These stale branches have already been removed from the remote repository referenced by <name>, but are still locally available in "remotes/".

With --dry-run option, report what branches will be pruned, but do not actually prune them.

like image 22
0b10011 Avatar answered Sep 23 '22 14:09

0b10011