重命名本地和远程 Git 存储库的 master 分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1526794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:10:18  来源:igfitidea点击:

Rename master branch for both local and remote Git repositories

gitgit-branchgit-pull

提问by Albert

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

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

I want to rename them to master-oldboth locally and on the remote. Is this possible?

我想将它们重命名为master-old本地和远程。这可能吗?

For other users who tracked origin/master(and who always updated their local masterbranch via git pull), what would happen after I renamed the remote branch?
Would their git pullstill work or would it throw an error that it couldn't find origin/masteranymore?

对于其他跟踪origin/master(并且总是master通过更新其本地分支git pull)的用户,在我重命名远程分支后会发生什么?
他们会git pull继续工作还是会抛出一个找不到的错误origin/master

Then, further on, I want to create a new masterbranch (both locally and remote). Again, after I did this, what would happen now if the other users do git pull?

然后,进一步,我想创建一个新master分支(本地和远程)。同样,在我这样做之后,如果其他用户这样做会发生什么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 masteras it is and create a new branch master-newand just work there further on?

我想这一切都会带来很多麻烦。有没有一种干净的方法来获得我想要的东西?或者我应该就这样离开master并创建一个新分支master-new并在那里进一步工作?

采纳答案by bdonlan

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 notattempt to track branch renames. If the new masterdoesn't exist yet, git pull will error out. If the new masterhas been created. the pull will attempt to merge masterand master-old. So it's generally a bad idea unless you have the cooperation of everyone who has checked out the repository previously.

然而,这有很多警告。首先,没有现成的检出会了解重命名-蹦也不会尝试跟踪分支重命名。如果新的master还不存在, git pull 会出错。如果新的master已经创建。拉将尝试合并mastermaster-old。所以这通常是一个坏主意,除非您有之前签出存储库的每个人的合作。

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.denyDeleteCurrentconfiguration value to warnor ignoreon the remoterepository. Otherwise, if you're ready to create a new master right away, skip the git push remote :masterstep, and pass --forceto the git push remote masterstep. Note that if you're not able to change the remote's configuration, you won't be able to completely delete the master branch!

注意:默认情况下,较新版本的 git 不允许您远程删除 master 分支。您可以通过在远程存储库中或在远程存储库上设置receive.denyDeleteCurrent配置值来覆盖它。否则,如果您准备立即创建新母版,请跳过该步骤并转到该步骤。请注意,如果您无法更改遥控器的配置,您将无法完全删除 master 分支!warnignoregit push remote :master--forcegit push remote master

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

这个警告只适用于当前分支(通常是master分支);任何其他分支都可以像上面一样删除和重新创建。

回答by Aristotle Pagaltzis

Assuming you are currently on master:

假设您目前在master

git push origin master:master-old        # 1
git branch master-old origin/master-old  # 2
git reset --hard $new_master_commit      # 3
git push -f origin                       # 4
  1. First make a master-oldbranch in the originrepository, based off of the mastercommit in the local repository.
  2. Create a new local branch for this new origin/master-oldbranch (which will automatically be set up properly as a tracking branch).
  3. Now point your local masterto whichever commit you want it to point to.
  4. Finally, force-change masterin the originrepository to reflect your new local master.
  1. 首先根据本地存储库中的提交在存储库中创建一个master-old分支。originmaster
  2. 为这个新origin/master-old分支创建一个新的本地分支(它将自动正确设置为跟踪分支)。
  3. 现在将您的本地指向您master希望它指向的任何提交。
  4. 最后,masterorigin存储库中强制更改以反映您的新本地master.

(If you do it in any other way, you need at least one more step to ensure that master-oldis properly set up to track origin/master-old. None of the other solutions posted at the time of this writing include that.)

(如果您以任何其他方式执行此操作,则至少需要再执行一个步骤以确保master-old正确设置以跟踪origin/master-old。在撰写本文时发布的其他解决方案均未包含此内容。)

回答by Excalibur

With Git v1.7, I think this has changed slightly. Updating your local branch's tracking reference to the new remote is now very easy.

使用 Git v1.7,我认为这略有改变。将本地分支的跟踪引用更新到新的远程现在非常容易。

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

回答by Treken

git checkout -b new-branch-name
git push remote-name new-branch-name :old-branch-name

You may have to manually switch to new-branch-namebefore deleting old-branch-name

您可能需要new-branch-name在删除之前手动切换到old-branch-name

回答by dnozay

There are many ways to rename the branch, but I am going to focus on the bigger problem: "how to allow clients to fast-forward and not have to mess with their branches locally".

重命名分支的方法有很多种,但我将关注更大的问题:“如何允许客户端快进,而不必在本地弄乱他们的分支”

First a quick picture: renaming master branch and allowing clients to fast-forward

先上一张快速图: renaming master branch and allowing clients to fast-forward

This is something actually easy to do; but don't abuse it. The whole idea hinges on merge commits; as they allow fast-forward, and link histories of a branch with another.

这实际上很容易做到;但不要滥用它。整个想法取决于合并提交;因为它们允许快进,并将一个分支的历史与另一个链接起来。

renaming the branch:

重命名分支:

# rename the branch "master" to "master-old"
# this works even if you are on branch "master"
git branch -m master master-old

creating the new "master" branch:

创建新的“主”分支:

# create master from new starting point
git branch master <new-master-start-point>

creating a merge commit to have a parent-child history:

创建合并提交以具有父子历史记录:

# now we've got to fix the new branch...
git checkout master

# ... by doing a merge commit that obsoletes
# "master-old" hence the "ours" strategy.
git merge -s ours master-old

and voila.

瞧。

git push origin master

This works because creating a mergecommit allows fast-forwardingthe branch to a new revision.

这是有效的,因为创建merge提交允许将分支快速转发到新修订。

using a sensible merge commit message:

使用合理的合并提交消息:

renamed branch "master" to "master-old" and use commit ba2f9cc as new "master"
-- this is done by doing a merge commit with "ours" strategy which obsoletes
   the branch.

these are the steps I did:

git branch -m master master-old
git branch master ba2f9cc
git checkout master
git merge -s ours master-old

回答by Cascabel

I'm assuming you're still asking about the same situation as in your previous question. That is, master-new will not contain master-old in its history.* If you call master-new "master", you will effectively have rewritten history. It does not matter howyou get into a state in which master is not a descendant of a previous position of master, simply that it is in that state.

我假设您仍在询问与上一个问题相同的情况。也就是说,master-new 在其历史中不会包含 master-old。* 如果你将 master-new 称为“master”,你将有效地重写历史。不管你如何进入一种状态,在这种状态中,主人不是以前主人的位置的后代,只是它处于那个状态。

Other users attempting to pull while master does not exist will simply have their pulls fail (no such ref on remote), and once it exists again in a new place, their pulls will have to attempt to merge their master with the new remote master, just as if you merged master-old and master-new in your repository. Given what you're trying to do here, the merge would have conflicts. (If they were resolved, and the result was pushed back into the repository, you'd be in an even worse state - both versions of history there.)

其他用户在 master 不存在的情况下尝试 pull 只会导致他们的 pull 失败(远程上没有这样的 ref),一旦它再次出现在新的地方,他们的 pull 将不得不尝试将他们的 master 与新的远程 master 合并,就像您在存储库中合并了 master-old 和 master-new 一样。鉴于您在这里尝试做的事情,合并会产生冲突。(如果它们得到解决,并且结果被推回存储库,您将处于更糟糕的状态 - 两个版本的历史记录都在那里。)

To answer your question simply: you should accept that sometimes there will be mistakes in your history. This is okay. It happens to everyone. There are reverted commits in the git.git repository. The important thing is that once we publish history, it is something everyone can trust.

简单地回答你的问题:你应该接受你的历史有时会出现错误。这没关系。它发生在每个人身上。git.git 存储库中有还原的提交。重要的是,一旦我们公布历史,它就是每个人都可以信任的东西。

*If it did, this would be equivalent to pushing some changes onto master, and then creating a new branch where it used to be. No problem.

*如果是这样,这相当于将一些更改推送到 master 上,然后在原来的位置创建一个新分支。没问题。

回答by Hendra Uzia

The selected answerfailed when I tried it. It throws an error: refusing to delete the current branch: refs/heads/master. I guess I'll post what works for me:

当我尝试时,选择的答案失败了。它抛出一个错误:refusing to delete the current branch: refs/heads/master。我想我会发布对我有用的内容:

git checkout master             # if not in master already

git branch placeholder          # create placeholder branch
git checkout placeholder        # checkout to placeholder
git push remote placeholder     # push placeholder to remote repository

git branch -d master            # remove master in local repository
git push remote :master         # remove master from remote repository.

The trick is to checkout to the placeholder right before pushing it to remote repository. The rest is self explanatory, deleting the master branch and push it to the remote repository should works now. Excerpted from here.

诀窍是在将占位符推送到远程存储库之前立即签出占位符。其余的不言自明,删除 master 分支并将其推送到远程存储库现在应该可以工作了。摘自这里

回答by Hendra Uzia

Good. My 2 cents. How about loggin in at the server, going to the git directory and renaming the branch in the bare repository. This does not have all the problems associated with reuploading the same branch. Actually, the 'clients' will automatically recognize the modified name and change their remote reference. Afterwards (or before) you can also modify the local name of the branch.

好的。我的 2 美分。如何登录服务器,转到 git 目录并重命名裸存储库中的分支。这并没有与重新上传同一分支相关的所有问题。实际上,“客户端”会自动识别修改后的名称并更改其远程引用。之后(或之前)您还可以修改分支的本地名称。

回答by Hannes Tydén

What about:

关于什么:

git checkout old-branch-name
git push remote-name new-branch-name
git push remote-name :old-branch-name
git branch -m new-branch-name

回答by Alireza

OK, renaming a branch both locallyand on the remoteis pretty easy!...

好的,在本地远程重命名分支非常简单!...

If you on the branch, you can easily do:

如果你在分支上,你可以很容易地做到:

git branch -m <branch>

git branch -m <branch>

or if not, you need to do:

否则,您需要执行以下操作:

git branch -m <your_old_branch> <your_new_branch>

git branch -m <your_old_branch> <your_new_branch>

Then, push deletion to the remote like this:

然后,像这样将删除推送到遥控器:

git push origin <your_old_branch>

git push origin <your_old_branch>

Now you done, if you get upstream error while you trying to push, simply do:

现在你完成了,如果你在尝试推送时遇到上游错误,只需执行以下操作:

git push --set-upstream origin <your_new_branch>

git push --set-upstream origin <your_new_branch>

I also create the image below to show the steps in real command line, just follow the steps and you would be good:

我还创建了下面的图像以显示实际命令行中的步骤,只需按照步骤操作即可:

enter image description here

enter image description here