重命名本地和远程 Git 存储库的 master 分支 [英] Rename master branch for both local and remote Git repositories
问题描述
我有分支 master
跟踪远程分支 origin/master
.
I have the branch master
which tracks the remote branch origin/master
.
我想在本地和远程将它们重命名为 master-old
.这可能吗?
I want to rename them to master-old
both locally and on the remote. Is this possible?
对于跟踪 origin/master
的其他用户(并且总是通过 git pull
更新他们本地的 master
分支),之后会发生什么我重命名了远程分支?
他们的 git pull
是否仍然有效,还是会抛出一个错误,无法找到 origin/master
了?
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?
然后,我想创建一个新的 master
分支(本地和远程).同样,在我这样做之后,如果其他用户执行 git pull
会发生什么?
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
?
我想这一切都会带来很多麻烦.有没有一种干净的方法来获得我想要的东西?或者我应该保留 master
原样并创建一个新分支 master-new
并在那里进一步工作?
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 recreating 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
然而,这有很多警告.首先,没有现有的检出会知道重命名 - Git 不会不尝试跟踪分支重命名.如果新的 master
还不存在,git pull 会出错.如果新的 master
已经创建.pull 将尝试合并 master
和 master-old
.所以这通常是一个坏主意,除非你有之前签出存储库的每个人的合作.
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.
注意:默认情况下,较新版本的 Git 将不允许您远程删除 master 分支.您可以通过在 remote 存储库上将 receive.denyDeleteCurrent
配置值设置为 warn
或 ignore
来覆盖它.否则,如果您准备立即创建新的 master,请跳过 git push remote :master
步骤,并将 --force
传递给 git push远程主
步骤.请注意,如果您无法更改遥控器的配置,您将无法完全删除 master 分支!
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!
这个警告只适用于当前分支(通常是master
分支);任何其他分支都可以像上面一样删除和重新创建.
This caveat only applies to the current branch (usually the master
branch); any other branch can be deleted and recreated as above.
这篇关于重命名本地和远程 Git 存储库的 master 分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!