重命名本地和远程Git存储库的主分支 [英] Rename master branch for both local and remote Git repositories

查看:110
本文介绍了重命名本地和远程Git存储库的主分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有跟踪远程分支 origin / master 的分支 master



我想在本地和远程将它们重命名为 master-old 。那可能吗?对于追踪 origin / master (以及谁经常通过更新其本地 master 分支)的其他用户, git pull ),重命名远程分支后会发生什么?他们的 git pull 仍然可以工作,还是会抛出一个错误,它找不到 origin / master 了? / p>

接下来,我想创建一个新的 master 分支(本地和远程)。同样,在我做完这些之后,如果其他用户做了 git pull



我会全部发生这会导致很多麻烦。有没有一种干净的方式来获得我想要的?或者,我应该照原样离开 master 并创建一个新的分支 master-new 并且在那里继续工作?

解决方案

重命名最接近的是删除并重新创建远程。例如:
$ b

git branch -m master master-old
git push remote: master#delete master
git push remote master-old#在远程

上创建master-old git checkout -b master some-ref#创建一个新的本地master
git push remote master#在远程

上创建主设备但是这有很多警告。首先,没有现有的结账会知道重命名 - git does not 试图跟踪分支重命名。如果新的 master 还不存在,那么git pull将会出错。如果新的 master 已被创建。 pull会尝试合并 master master-old 。所以这通常是一个糟糕的主意,除非你有与之前检出版本库的人合作。



注意:较新版本的git不允许删除master默认远程分支。您可以通过将 receive.denyDeleteCurrent 配置值设置为 warn 忽略 存储库上执行code>。否则,如果您准备立即创建一个新的主文件,请跳过 git push remote:master 步骤,并传递 - force git push remote master 步骤。请注意,如果您无法更改远程的配置,您将无法完全删除主分支!



此警告仅适用于当前分支(通常是 master 分支);任何其他分支可以删除并重新创建。


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 closest thing to renaming is deleting and then re-creating on the remote. For example:

git branch -m master master-old
git push remote :master         # delete master
git push remote master-old      # create master-old on remote

git checkout -b master some-ref # create a new local master
git push remote master          # create master on remote

However this has a lot of caveats. First, no existing checkouts will know about the rename - git does not attempt to track branch renames. If the new master doesn't exist yet, git pull will error out. If the new master has been created. the pull will attempt to merge master and master-old. So it's generally a bad idea unless you have the cooperation of everyone who has checked out the repository previously.

Note: Newer versions of git will not allow you to delete the master branch remotely by default. You can override this by setting the receive.denyDeleteCurrent configuration value to warn or ignore on the remote repository. Otherwise, if you're ready to create a new master right away, skip the git push remote :master step, and pass --force to the git push remote master step. Note that if you're not able to change the remote's configuration, you won't be able to completely delete the master branch!

This caveat only applies to the current branch (usually the master branch); any other branch can be deleted and recreated as above.

这篇关于重命名本地和远程Git存储库的主分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆