小编典典

将当前 Git 分支设为 master 分支

git

我在 Git 中有一个存储库。我做了一个分支,然后对主分支和分支进行了一些更改。

然后,数十次提交后,我意识到分支的状态比 master 好得多,所以我希望分支“成为”master 而忽略 master 上的更改。

我无法合并它,因为我不想将更改保留在 master 上。我该怎么办?

额外:在这种情况下,“旧”主服务器已经被push编辑到另一个存储库,例如 GitHub。这如何改变事情?


阅读 414

收藏
2022-02-14

共1个答案

小编典典

其他两个答案的问题是新主人没有老主人作为祖先,所以当你推它时,其他人都会乱七八糟。这就是你想要做的:

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 merge --strategy=ours --no-commit master
git commit          # add information to the template merge message
2022-02-14