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

Time:09-21

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.

CodePudding user response:

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.

CodePudding user response:

SOLVED

The problem I got came out not from a technical issue but from a logical/practical issue:

If you are working on a branch, in this case develop, containing some changes to your AS IS/"standard" code (let's say the AS IS code lies in master branch) there is no sense in developing other branches named develop/added-something, develop/added-something else, etc. because then the changes in develop would not have any "specification" characterizing them (something like /specification-of-the-changes).

This is properly forbidden by git, that, having an existing branch changes, does not allow to have branches named like changes/specification.

Not functional (and forbidden by git):

develop/added-element1
develop/added-element2
develop/added-element3
develop # so what characterize this branch?

Instead, one should have a repository where the AS IS code lies (let's say master) and then many branches where the code with changes in progress lies, and these branches should follow a naming convention like develop/added-something, develop/added-something-else, where the feature that characterize them is clearly resumed in the branch name.

Functional (and allowed by git):

develop/added-element1
develop/added-element2
develop/added-element3

  •  Tags:  
  • git
  • Related