Search code examples
gitgithubgit-branchgit-push

Cannot push branch after renaming and fixing error with update-refs


I want to rename my local branch from sort-rows-of-common-tables to develop/sort-rows-of-common-tables, and then push it on remote as new branch. I already have a local and remote branch named develop.

So I tryed with

git branch -m develop/sort-rows-of-common-tables

but I got

error: 'refs/heads/develop' exists; cannot create 'refs/heads/develop/sort-rows-of-common-tables'

so I read this SO answer and succeeded to solve the problem by running

git update-ref -d refs/heads/develop
git branch -m develop/sort-rows-of-common-tables

Now I want to push develop/sort-rows-of-common-tables branch on remote, where this branch does not exists yet, while develop branch already exixts.

So I try to run

git push origin develop/sort-rows-of-common-tables

but I get

Enumerating objects: 7, done.    
Counting objects: 100% (7/7), done.    
Delta compression using up to 4 threads    
Compressing objects: 100% (4/4), done.    
Writing objects: 100% (4/4), 607 bytes | 607.00 KiB/s, done.    
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0    
remote: error: cannot lock ref 'refs/heads/develop/sort-rows-of-common-tables': 'refs/heads/develop' exists; cannot create 'refs/heads/develop/sort-rows-of-common-tables'    
To my_git_repo.git    
 ! [remote rejected] develop/sort-rows-of-common-tables -> develop/sort-rows-of-common-tables (failed to update ref)    
error: failed to push some refs to 'my_git_repo.git'

It looks like I get the same error I was getting on local, so I bet that in order to fix it on remote I have to run a command similar to the one I run before (git update-ref -d refs/heads/develop) which targets the remote refs.

Is it correct? If yes, how can I update the remote refs?

I have searched git documentation on the matter but with no success.


Solution

  • It looks like I get the same error I was getting on local,

    Yes, exactly.

    so I bet that in order to fix it on remote I have to run a command similar to the one I run before (git update-ref -d refs/heads/develop) which targets the remote refs.

    Almost. You cannot run exactly this command on the remote side. But you can remove the remote branch with a push:

    git push origin :develop
    

    or

    git push origin --delete develop
    

    See the docs on :dst and --delete.