Git branch rename (non-master) - Atlassian Stash

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

But no matter what I do, renaming the branch does not reflect to the 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 'current' branch

git branch -m SR5678

      

Then tried to rename the branch on the 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 problem is, on the remote machine (hosted on the atlassian stash), I can see the old branch name, no matter what I do. Local only, I see the new branch name.

How do I change the name of a remote branch?

+3


source to share


3 answers


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

# 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...

By doing this, if more than one person is working on the repo, you must inform all employees of the changes. If not, they can recreate your old branch by pushing their local history (which still contains a link):

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

      



If a local update cannot be done (the local branch does not have the same name as the original), git will inform users of the checkout with a message like:

Your branch is based on "..." but upstream is not.

But it will still use the old story next git push

, unless the user is handling the rename locally (and manually).

So, you should rename branches remotely ONLY if you really need to to prevent problems , or if you just pushed to the original (assuming no one else has pulled since then).

+14


source


I think the problem is due to your .git/config

file variable branch.SR5678.merge

.

Steps you followed to rename a branch

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

      

So after 1 and 2 commands, the local branch name was changed, but the variable merge

still refers to the earlier branch name. as shown below:



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

      

And this branch name is used for Push

to or commands Pull

. Hence your 3rd command will still embed the old named branch for the remote location.

For solutions, you can use any of the below:

[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

      

+1


source


When you pushed your branch, it still knows about the tracking branch Sr1234

. When you push nothing on a branch on :BRANCH

, you delete that branch on the remote.

To create a branch on the remote you can specify which branch you want to push to

git push --set-upstream origin SR5687:SR5687

      

As part of your move that the old branch deleted, it might be a good idea to delete the old name in the remote repository as you no longer need it.

0


source







All Articles