git checkout better_branch
git merge --strategy=ours master # keep the content of this branch, but record a merge
git checkout master
git merge better_branch # fast-forward master up to the merge
git checkout new-master
git branch -m master old-master
git branch -m new-master master
# And don't do this part. Just don't. But if you want to...
# git branch -d --force old-master
没有合并,没有长命令 . 只需 branch 和 push - 但是,是 this will rewrite history 的 master 分支,所以如果你在一个团队工作,你必须知道你在做什么 .
或者,我发现您可以将任何分支推送到任何远程分支,因此:
# This will force push the current branch to the remote master
git push -f origin HEAD:master
# Switch current branch to master
git checkout master
# Reset the local master branch to what's on the remote
git reset --hard origin/master
Objective: Make current state of "branch" the "master"
在分支上工作,提交并推送您的更改以确保您的本地和远程存储库是最新的:
git checkout master # Set local repository to master
git reset --hard branch # Force working tree and index to branch
git push origin master # Update remote repository
在此之后,您的主服务器将是您上次提交分支的确切状态,您的主提交日志将显示该分支的所有签入 .
0
要添加到Jefromi 's answer, if you don' t想要在 source 分支的历史记录中放置无意义的合并,您可以为 ours 合并创建一个临时分支,然后将其丢弃:
git checkout <source>
git checkout -b temp # temporary branch for merge
git merge -s ours <target> # create merge commit with contents of <source>
git checkout <target> # fast forward <target> to merge commit
git merge temp # ...
git branch -d temp # throw temporary branch away
git checkout <source> # fill index with contents of <source>
git symbolic-ref HEAD <target> # tell git we're committing on <target>
git commit -m "Setting contents to <source>" # make an ordinary commit with the contents of <source>
-1
我在博客文章_634466中找到了我想要的答案:
git checkout feature_branch
git merge -s ours --no-commit master
git commit # Add a message regarding the replacement that you just did
git checkout master
git merge feature_branch
13 回答
另外两个答案的问题是新主人没有旧主人作为祖先,所以当你推动它时,其他人都会搞砸了 . 这是你想要做的:
如果您希望您的历史记录更清晰一些,我建议您在合并提交消息中添加一些信息,以明确您所做的事情 . 将第二行更改为:
确保所有内容都被推送到远程存储库(GitHub):
用“better_branch”覆盖“master”:
强制推送到远程存储库:
Edit: 你没有说你曾经推动过公共回购!这让世界变得与众不同 .
有两种方式,"dirty"方式和"clean"方式 . 假设您的分支名为
new-master
. 这是干净的方式:这将使配置文件更改以匹配重命名的分支 .
你也可以用脏方式来做,这不会更新配置文件 . 这就是上面介绍的内容......
通过以下方式将分支重命名为
master
:这里给出的解决方案(在'master'中重命名分支)并不坚持远程(GitHub)repo的后果:
如果你在制作那个分支后没有推动任何东西,你可以重命名它并毫无问题地推动它 .
如果你在GitHub上有push master,你需要'git push -f'新分支:you can no longer push in a fast forward mode .
如果其他人已经撤回了你的仓库,他们将无法在没有用新的GitHub主分支替换他们自己的主仓(或处理大量合并)的情况下拉出新的主历史记录 .
有alternatives to a git push --force for public repos .
Jefromi's answer(将正确的更改合并回原始主控)就是其中之一 .
据我所知,您可以将当前分支分支到现有分支 . 从本质上讲,这将覆盖
master
与当前分支中的任何内容:一旦你完成了,你通常可以推送你的本地
master
分支,也可能需要force参数:没有合并,没有长命令 . 只需
branch
和push
- 但是,是 this will rewrite history 的master
分支,所以如果你在一个团队工作,你必须知道你在做什么 .或者,我发现您可以将任何分支推送到任何远程分支,因此:
也可以将其他分支中的所有文件签出到master中:
然后提交所有更改 .
我发现这种简单的方法可以发挥最佳效果 . 它不会重写历史记录,并且分支的所有先前签入都将附加到主服务器 . 没有任何东西丢失,你可以清楚地看到提交日志中发生的事情 .
Objective: Make current state of "branch" the "master"
在分支上工作,提交并推送您的更改以确保您的本地和远程存储库是最新的:
在此之后,您的主服务器将是您上次提交分支的确切状态,您的主提交日志将显示该分支的所有签入 .
要添加到Jefromi 's answer, if you don' t想要在
source
分支的历史记录中放置无意义的合并,您可以为ours
合并创建一个临时分支,然后将其丢弃:这样,合并提交将仅存在于
target
分支的历史记录中 .或者,如果您根本不想创建合并,则只需获取
source
的内容并将其用于target
上的新提交:我在博客文章_634466中找到了我想要的答案:
它与Cascabel's answer基本相同 . 除了他在下面添加的"option",他的解决方案已嵌入我的主代码块中 .
找到这种方式更容易 .
我将此添加为新答案,因为如果我以后需要此解决方案,我希望在一个代码块中使用我将要使用的所有代码 .
否则,我可以复制粘贴,然后阅读下面的详细信息以查看我应该更改的行 - 在我已经执行之后 .
我的做事方式如下
如果您在 Eclipse 中使用 eGit :
右键单击项目节点 .
选择团队→然后选择高级→然后重命名分支
然后展开 remote tracking 文件夹 .
选择名称错误的分支,然后单击重命名按钮,将其重命名为新名称 .
选择新的主服务器,然后将其重命名为master .
以下步骤在由Atlassian(Bitbucket服务器)提供支持的Git浏览器中执行
将设为
master
从
master
中创建一个分支,并将其命名为“master-duplicate” .从中创建一个分支并将其命名为“ -copy” .
在存储库设置(Bitbucket)中,将“Default Branch”更改为指向“master-duplicate”(没有此步骤,您将无法删除master - “在下一步”) .
删除“master”分支 - 我从源代码树执行了此步骤(您可以从CLI或Git浏览器执行此操作)
将“”重命名为“master”并推送到存储库(这将创建一个新的“master”分支,仍然存在“” .
在存储库设置中,将“默认分支”更改为指向“主” .