如何从另一个分支完全替换 Git 中的 master 分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2862590/
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
How to replace master branch in Git, entirely, from another branch?
提问by Jason
I have two branches in my Git repository:
我的 Git 存储库中有两个分支:
master
seotweaks
(created originally frommaster
)
master
seotweaks
(最初创建于master
)
I created seotweaks
with the intention of quickly merging it back into master
. However, that was three months ago and the code in this branch is 13 versions ahead of master
.
我创建seotweaks
的目的是将它快速合并回master
. 然而,那是三个月前的事了,这个分支的代码比master
.
It has effectively become our working master branch as all the code in master
is more or less obsolete now.
它实际上已经成为我们工作的主分支,因为其中的所有代码master
现在或多或少已经过时了。
Very bad practice I know, lesson learned.
我知道非常糟糕的做法,吸取了教训。
Do you know how I can replace all of the contents of the master
branch with those in seotweaks
?
你知道我如何用 . 中的内容替换master
分支的所有内容seotweaks
吗?
I could just delete everything in master
and merge, but this does not feel like best practice.
我可以删除所有内容master
并合并,但这并不是最佳实践。
回答by ergosys
You should be able to use the "ours" merge strategy to overwrite master with seotweaks like this:
您应该能够使用“我们的”合并策略来覆盖 master 与 seotweaks 这样的:
git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks
The result should be your master is now essentially seotweaks.
结果应该是你的主人现在基本上是seotweaks。
(-s ours
is short for --strategy=ours
)
(-s ours
的缩写--strategy=ours
)
From the docsabout the 'ours' strategy:
来自关于“我们的”策略的文档:
This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.
这可以解析任意数量的头,但合并的结果树始终是当前分支头的树,有效地忽略了所有其他分支的所有更改。它旨在用于取代旧的侧枝发展历史。请注意,这与递归合并策略的 -Xours 选项不同。
Update from comments: If you get fatal: refusing to merge unrelated histories
, then change the second line to this: git merge --allow-unrelated-histories -s ours master
从评论更新:如果你得到了致命的:refusing to merge unrelated histories
,然后将第二行更改为:gitmerge --allow-unrelated-histories -s ours master
回答by ZelluX
What about using git branch -m to rename the master branch to another one, then rename seotweaks branch to master? Something like this:
使用 git branch -m 将 master 分支重命名为另一个分支,然后将 seotweaks 分支重命名为 master 怎么样?像这样的东西:
git branch -m master old-master
git branch -m seotweaks master
git push -f origin master
This might remove commits in origin master, please check your origin master before running git push -f origin master
.
这可能会删除 origin master 中的提交,请在运行前检查您的 origin master git push -f origin master
。
回答by VonC
You can rename/remove master on remote, but this will be an issue if lots of people have based their work on the remote master branch and have pulled that branch in their local repo.
That might not be the case here since everyone seems to be working on branch 'seotweaks
'.
您可以在远程重命名/删除 master,但是如果很多人将他们的工作基于远程 master 分支并将该分支拉到他们的本地 repo 中,这将是一个问题。
情况可能并非如此,因为每个人似乎都在分支“ seotweaks
”上工作。
In that case you can:
git remote --show may not work.
(Make a git remote show
to check how your remote is declared within your local repo. I will assume 'origin
')
(Regarding GitHub, house9comments: "I had to do one additional step, click the 'Admin
' button on GitHub and set the 'Default Branch
' to something other than 'master
', then put it back afterwards")
在这种情况下,您可以:
git remote --show 可能不起作用。(git remote show
检查一下你的遥控器是如何在你的本地存储库中声明的。我将假设 ' origin
')
(关于 GitHub,house9评论:“我必须做一个额外的步骤,点击Admin
GitHub 上的 ' ' 按钮并将 ' Default Branch
'设置为' master
'以外的东西,然后把它放回去")
git branch -m master master-old # rename master on local
git push origin :master # delete master on remote
git push origin master-old # create master-old on remote
git checkout -b master seotweaks # create a new local master on top of seotweaks
git push origin master # create master on remote
But again:
但是再次:
- if other users try to pull while master is deleted on remote, their pulls will fail ("no such ref on remote")
- when master is recreated on remote, a pull will attempt to merge that new master on their local (now old) master: lots of conflicts. They actually need to
reset --hard
their local master to the remote/master branch they will fetch, and forget about their current master.
- 如果其他用户在远程删除 master 时尝试拉取,他们的拉取将失败(“远程上没有这样的引用”)
- 当在远程重新创建 master 时,pull 将尝试将新的 master 合并到他们本地(现在是旧的)master 上:很多冲突。他们实际上需要
reset --hard
他们的本地 master 到他们将要获取的远程/master 分支,而忘记了他们当前的 master。
回答by mholm815
Since seotweaks
was originally created as a branch from master
, merging it back in is a good idea. However if you are in a situation where one of your branches is not really a branch from master
or your history is so different that you just want to obliterate the master
branch in favor of the new branch that you've been doing the work on you can do this:
由于seotweaks
最初是作为来自 的分支创建的master
,因此将其合并回是一个好主意。但是,如果您处于这样一种情况,其中您的一个分支不是真正的分支,master
或者您的历史如此不同,以至于您只想删除master
分支以支持您一直在做的工作的新分支,那么您可以做这个:
git push [-f] origin seotweaks:master
This is especially helpful if you are getting this error:
如果您收到此错误,这将特别有用:
! [remote rejected] master (deletion of the current branch prohibited)
And you are not using GitHub and don't have access to the "Administration" tab to change the default branch for your remote repository. Furthermore, this won't cause down time or race conditions as you may encounter by deleting master:
而且您没有使用 GitHub,也无权访问“管理”选项卡来更改远程存储库的默认分支。此外,这不会导致您在删除 master 时可能遇到的停机时间或竞争条件:
git push origin :master
回答by ScottGuymer
I found this to be the best way of doing this (I had an issue with my server not letting me delete).
我发现这是最好的方法(我的服务器有问题,不允许我删除)。
On the server that hosts the origin
repository, type the following from a directory inside the repository:
在托管origin
存储库的服务器上,从存储库内的目录中键入以下内容:
git config receive.denyDeleteCurrent ignore
On your workstation:
在您的工作站上:
git branch -m master vabandoned # Rename master on local
git branch -m newBranch master # Locally rename branch newBranch to master
git push origin :master # Delete the remote's master
git push origin master:refs/heads/master # Push the new master to the remote
git push origin abandoned:refs/heads/abandoned # Push the old master to the remote
Back on the server that hosts the origin
repository:
返回托管origin
存储库的服务器:
git config receive.denyDeleteCurrent true
Credit to the author of blog post http://www.mslinn.com/blog/?p=772
感谢博客文章的作者http://www.mslinn.com/blog/?p=772