Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

git - 重命名本地和远程Git存储库的master分支(Rename master branch for both local and remote Git repositories)

I have the branch master which tracks the remote branch origin/master .

(我有分支master追踪远程分支origin/master 。)

I want to rename them to master-old both locally and on the remote.

(我想将它们重命名为本地和远程master-old 。)

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?

(对于跟踪origin/master (并且总是通过git pull更新其本地master git pull )的其他用户,重命名远程分支后会发生什么?)

Would their git pull still work or would it throw an error that it couldn't find origin/master anymore?

(他们的git pull仍然可以工作还是会抛出错误,导致找不到origin/master了?)

Then, further on, I want to create a new master branch (both locally and remote).

(然后,进一步,我想创建一个新的master分支(本地和远程)。)

Again, after I did this, what would happen now if the other users do git pull ?

(同样,在我完成此操作之后,如果其他用户执行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?

(还是我应该只保留master并创建一个新的master-new分支,然后继续工作?)

  ask by Albert translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(首先,没有现成的检出会了解重命名- Git 并不试图跟踪分支重命名。)

If the new master doesn't exist yet, git pull will error out.

(如果新的master服务器不存在,则git pull将出错。)

If the new master has been created.

(是否创建了新的master 。)

the pull will attempt to merge master and master-old .

(拉将尝试合并mastermaster-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.

(注意:默认情况下,较新版本的git不允许您远程删除master分支。)

You can override this by setting the receive.denyDeleteCurrent configuration value to warn or ignore on the remote repository.

(您可以通过将receive.denyDeleteCurrent配置值设置为warnignore 远程存储库来覆盖此设置。)

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.

(否则,如果您准备立即创建新的master,则跳过git push remote :master步骤,并将--force传递给git push remote master步骤。)

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);

(此警告仅适用于当前分支(通常是master分支);)

any other branch can be deleted and recreated as above.

(可以如上所述删除和重新创建任何其他分支。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...