本文翻译自:How to replace master branch in Git, entirely, from another branch? [duplicate]
Possible Duplicate: 可能重复:
Make the current Git branch a master branch 将当前的Git分支设为主分支
I have two branches in my Git repository: 我的Git存储库中有两个分支:
-
master
-
seotweaks
(created originally frommaster
)seotweaks
(最初由master
创建)
I created seotweaks
with the intention of quickly merging it back into master
. 我创建了seotweaks
,旨在将其快速合并回master
。 However, that was three months ago and the code in this branch is 13 versions ahead of master
. 但是,那是三个月前的事,该分支中的代码比master
早13个版本。
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
? 您知道我如何用seotweaks
中的内容替换master
分支的所有内容吗?
I could just delete everything in master
and merge, but this does not feel like best practice. 我只可以删除master
并合并中的所有内容,但这并不是最佳实践。
#1楼
参考:https://stackoom.com/question/C0go/如何从另一个分支完全替换Git中的master分支-重复
#2楼
Since seotweaks
was originally created as a branch from master
, merging it back in is a good idea. 由于seotweaks
最初是作为master
一个分支创建的,因此将其合并回是个好主意。 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: 但是,如果您处在其中一个分支实际上不是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. 而且您没有使用GitHub,也无权访问“管理”选项卡来更改远程存储库的默认分支。 Furthermore, this won't cause down time or race conditions as you may encounter by deleting master: 此外,这不会像您删除母版时可能遇到的停机或竞赛情况那样:
git push origin :master
#3楼
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
#4楼
What about using git branch -m to rename the master branch to another one, then rename seotweaks branch to master? 使用git branch -m将master分支重命名为另一个分支,然后将seotweaks分支重命名为master呢? Something like this: 像这样:
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
. 这可能会删除原始主机中的提交 ,请在运行git push -f origin master
之前检查git push -f origin master
。
#5楼
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
'. 此处可能并非如此,因为每个人似乎都在致力于“ seotweaks
”分支。
In that case you can: 在这种情况下,您可以:
git remote --show may not work. git remote --show可能不起作用。 (Make a git remote show
to check how your remote is declared within your local repo. I will assume ' origin
') (进行git remote show
检查您的本地仓库中如何声明您的remote。我将假定为“ origin
”)
(Regarding GitHub, house9 comments: "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") (关于GitHub, house9评论:“我还必须执行另一步骤,单击GitHub上的“ Admin
”按钮,并将“ 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") 如果其他用户在远程删除master时尝试拉,则他们的拉取将失败(“在远程上没有这样的引用”)
- when master is recreated on remote, a pull will attempt to merge that new master on their local (now old) master: lots of conflicts. 在远程上重新创建master时,拉动将尝试将该新master合并到其本地(现在是旧的)master上:冲突很多。 They actually need to
reset --hard
their local master to the remote/master branch they will fetch, and forget about their current master. 实际上,他们需要reset --hard
其本地主服务器reset --hard
为将要获取的remote / master分支,而忘记了当前的主服务器。
#6楼
You should be able to use the "ours" merge strategy to overwrite master with seotweaks like this: 您应该能够使用“我们的”合并策略,用seotweaks覆盖master,如下所示:
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 docs about 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. 这样可以解析任意数量的head,但是合并的结果树始终是当前分支head的树,有效地忽略了所有其他分支的所有更改。 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
,然后将第二行更改为:git merge --allow-unrelated-histories -s ours master