Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git refuses to remove a branch

Tags:

git

github

I have a single branch on this GitHub repository (which is not master) and now I'm trying to rename it and for some reason I am getting this error:

git push origin :OldName
To [email protected]:user/gitRepo.git
 ! [remote rejected] OldName (refusing to delete the current branch: refs/heads/OldName)
error: failed to push some refs to '[email protected]:user/gitRepo.git'

Is this because I don't have any master branch? Here's my git branch -a:

OldName
remotes/origin/HEAD -> origin/OldName
remotes/origin/OldName
like image 960
Yar Avatar asked Feb 07 '23 14:02

Yar


2 Answers

This is not a [git] restriction, but [github] doing an extra layer of sanity protection for you. Change the [default branch] in the GitHub project of yours, and then try again.

Sample URL (change [user] and [project]):

https://github.com/[user]/[project]/settings/branches

like image 108
starlocke Avatar answered Feb 09 '23 11:02

starlocke


If the output from git branch -a be accurate, then it appears you only have a single branch OldName, and single corresponding branch by the same name on GitHub. It doesn't make much sense for you to be deleting the only branch in the repository, because then there would be no content, nothing, in that repository (except for maybe the reflog, if that still exists).

Specifically, the error is likely being caused for a slightly different reason. Since the OldName branch is the only branch in GitHub, is also most likely the default branch for your repository. GitHub will not allow you to delete the default branch, because then it means that someone browsing your repo might not be able to see content.

To get around this problem, you can create a branch with the name you want, and do the deletions from there:

Locally

git checkout -b NewName  # create a dummy branch from OldName
git push origin NewName

On GitHub

Change the default branch to NewName

Locally (again):

git push origin :OldName
like image 31
Tim Biegeleisen Avatar answered Feb 09 '23 11:02

Tim Biegeleisen