Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git branch rename (non-master) - Atlassian Stash

Tags:

git

I renamed a local branch (non-master) and then wanted to rename the remote barnch (Atlassian stash hosted repository).

But, no matter what I do the branch rename is not reflected on remote.

This is what I did

git branch SR1234
git checkout SR1234
git push --set-upstream origin SR1234
git add --all
git commit -m "...."
git push

Then renamed the 'current' branch

git branch -m SR5678

Then tried to rename the branch on remote

git push origin :SR1234
$ git push origin :SR1234
To ssh://<repo>.git
 - [deleted]         SR1234


$ git push --set-upstream origin SR5678
Total 0 (delta 0), reused 0 (delta 0)
To ssh://<repo>.git
 * [new branch]      SR5678 -> Sr1234
Branch SR5678 set up to track remote branch SR1234 from origin.

The trouble is that on remote (hosted on atlassian stash), I see the old branch name no matter what I do. Only local, I see the new branch name.

How can I change the remote branch name?

like image 284
koolkid Avatar asked Dec 01 '22 17:12

koolkid


2 Answers

The complete way to rename a branch locally and remotely is something like:

# rename it locally
git branch -m old-name new-name

# push the new one remotely
git push --set-upstream origin new-name

# delete the old one remotely
git push origin :old-name

BUT ...

Doing so, if more than one person is working on the repo, you must inform all workers about your changes. If not, they could recreate your old branch by pushing their local history (which still contains its reference):

# hardly refresh a local clone if possible
git fetch --prune --all

If the local update can not be done (the local branch does not have the same name as your original one for instance), git will inform users on checkout with a message like:

Your branch is based on '...', but the upstream is gone.

But it will still push the old history on next git push if the user does not process the rename locally (and manually).

So you should remotely rename branches ONLY if it is really required to avoid problems or if you just pushed the original one (assuming no one pulled from that time).

like image 193
picas Avatar answered Dec 19 '22 23:12

picas


I think the problem occurs due to your .git/config file's branch.SR5678.merge variable.

Steps you followed for renaming a branch

1) git branch -m SR5678
2) git push origin :SR1234
3) git push --set-upstream origin SR5678

So after 1 & 2 commands, your local branch name has been changed but the merge variable still refers to the older branch name. like below:

[branch "SR5678"]
     remote = origin
     merge = refs/heads/SR1234

And this branch name is used for Push to or Pull from commands. Hence, your 3rd command will still push into old named branch for a remote location.

Any of below can be used for solutions:

[1]
git branch -m SR1234 SR5678
git push origin :SR1234 
git push -u origin SR5678:SR5678

[2]
git branch -m SR1234 SR5678
git push origin :SR1234 
git push --set-upstream origin SR5678:SR5678

[3]
git branch -m SR1234 SR5678
git push origin :SR1234 
git config branch.SR5678.merge refs/heads/SR5678
git push -u origin SR5678
like image 44
Chetan Ghodasara Avatar answered Dec 19 '22 22:12

Chetan Ghodasara