标签云

微信群

扫码加入我们

WeChat QR Code

I have the branch master which tracks the remote branch origin/master.I want to rename them to master-old both locally and on the remote. Is that possible? For other users who tracked origin/master (and who always updated their local master branch via git pull), what would happen after I renamed the remote branch? Would their git pull still work or would it throw an error that it couldn't find origin/master anymore?Then, further on, I want to create a new master branch (both locally and remote). Again, after I did this, what would happen now if the other users do git pull?I guess all this would result in a lot of trouble. Is there a clean way to get what I want? Or should I just leave master as it is and create a new branch master-new and just work there further on?


The recipe given in the accepted answer does apply to a branch of any name, but the caveats (as noted) do not, due to the (by default) special role of the master branch in Git.

2019年03月26日47分43秒

kynan: I think I don't understand. What caveats do apply to master and don't apply on other branches? If it would be a branch named xy and other people have tracked that branch, how would that be different?

2019年03月26日47分43秒

The caveat that you can't normally delete the remote master. That does not apply to Aristotle's answer though, so you might want to mark that as the accepted answer. You're correct, any git push -f affects the ability to pull from any remote tracking branch.

2019年03月26日47分43秒

you can create a new branch master-old that points to the same commit as the previous master branch. Then you can overwrite the master branch with your new changes by doing a merge with the ours strategy. Doing a merge works when the remote does not allow non-fastforward changes. That also means other users won't have forced updates.

2019年03月26日47分43秒

kynan master is only special as long as it's the only existing branch. As soon as you have more than one, all branches are on an equal footing.

2019年03月26日47分43秒

branches are just a (name, hash) pair - nothing more, nothing less. There is the reflog on branches, but this is never exposed to remote clients.

2019年03月26日47分43秒

I would create master-old on remote before deleting master on remote. I'm just paranoid.

2019年03月27日47分43秒

Aristotle's answer below allows you to do this without deleting the master, so I'd think that preferable.

2019年03月27日47分43秒

it would be clear and SAFE if you can use new-branch-name and old-branch-name instead of master/master-old, thus this is a general problem.

2019年03月27日47分43秒

If the deleted branch (here: master) is not referenced by other branches, git might garbage collect all commits on that ... well ... “branch”. – Some git porcelain commands trigger a garbage collection. – Therefore: create the new name first (pointing to the same commit), then delete the old name.

2019年03月27日47分43秒

This is better answer than "the answer", I agree, but for people who came here to just rename a branch (not explicitly master), the 3rd step doesn't make much sense.

2019年03月27日47分43秒

It makes absolutely no difference to the answer whether you are on master or another branch. The question was badly titled though, it asks about a task more complex than just renaming a branch.

2019年03月26日47分43秒

This turned out to be the solution that worked for me.I was trying to replace master with another branch.I did a git log -1 origin/what_i_want_as_new_master to get the $new_master_commit for step 3. After the push (step 4), other devs would pull and get messages "your branch is ahead of master by 295 commits." To fix this I sent out an email letting them know to each run: git pull; git checkout some_random_branch; git branch -D master; git pull; git checkout master;Basically, they need to remove their local master and pull the new version otherwise they're at the wrong place locally.

2019年03月27日47分43秒

You could have done that far more easily: assuming they’re already on master then they could just do git fetch && git reset --hard origin/master to force their local master to be the same as the one on origin. I have documented this, as well as the more complex case where you have local commits on top of master that you want to keep, in stackoverflow.com/q/4084868

2019年03月26日47分43秒

Make sure that the remote config file has "denyNonFastforwards = false" or you will get "remote: error: denying non-fast-forward refs/heads/master (you should pull first)"

2019年03月26日47分43秒

An alternative to --set-upstream is as follows: Once you have your branch renamed locally and deleted on the origin, simply do:git push -u --all

2019年03月26日47分43秒

This will not work with master branch, since git will not allow you to delete the remote master.

2019年03月27日47分43秒

AlexandreNeto In this case you can execute the 3rd line before the 2nd, set the default branch to new_branch and then eventually delete the remote master with the 2nd line.

2019年03月27日47分43秒

Amazingly simple steps. This is the best answer of the question

2019年03月26日47分43秒

To delete the remote branch git push origin --delete old_branch is slightly more readable.

2019年03月26日47分43秒

Does any part of this solution delete the local old-branch-name, or is that a serparate exercise?

2019年03月27日47分43秒

I thinks at the end must be run git branch -d old-branch-name for delete local old branch.

2019年03月26日47分43秒

You can push changes by only one command: git push remote-name new-branch-name :old-branch-name.

2019年03月26日47分43秒

This way will no you complicate the git history? Because you are opening a new branch instead just renaming the current one.

2019年03月27日47分43秒

androider No. Branches in git is a simple references.

2019年03月27日47分43秒

Thanks! git merge -s ours master-old is the crucial piece that the other answers miss. Also, "easy to do" does not mean "easy to understand, or find out" which seems to be the case with much of git, but I digress.

2019年03月26日47分43秒

I love the fact that no deletions are mentioned and that the transition for those working with clones of upstream is "seamless". Thank you!

2019年03月27日47分43秒

Yea, it's the same problem, just was one idea how to solve it. But even if I would not do this branch-renaming, I was interesting if it would be possible.I thought such refs as "master" are only references to specific commits. I really don't want to change any history. I thought I would just point the master-reference to another head.This also means, I can never ever use a branch name again if I have ever used it before?

2019年03月26日47分43秒

Indeed, branches are refs - pointers to commits. The thing is, we expect the head of a branch to evolve in a particular way (namely, always fast-forwarding). From the point of view of someone else, moving a branch in your public repo is the same as rewriting the history of the branch. It no longer points to a commit containing everything it used to.

2019年03月26日47分43秒

It would fail on git push remote :master if this is checked on remote side - you will see "remote: error:" as prefix in the error log lines.

2019年03月27日47分43秒

I have forgotten the credentials to log on to the github server. Anybody with credentials out there :-P

2019年03月26日47分43秒

messes up with branch tracking - users may have to fix their branch locally?

2019年03月27日47分43秒

This doesn't seem to work for me, I get:git update-ref trunk trunk2 fatal: trunk2: not a valid SHA1

2019年03月27日47分43秒