将当前 Git 分支设为 master 分支 [英] Make the current Git branch a master branch

查看:58
本文介绍了将当前 Git 分支设为 master 分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Git 中有一个存储库.我创建了一个分支,然后对 master 和分支都做了一些更改.

I have a repository in Git. I made a branch, then did some changes both to the master and to the branch.

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

Then, tens of commits later, I realized the branch is in much better state than the master, so I want the branch to "become" the master and disregard the changes on master.

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

I cannot merge it, because I don't want to keep the changes on master. What should I do?

额外:在这种情况下,旧"master 已经push 到另一个存储库,例如 GitHub.这会如何改变事情?

Extra: In this case, the 'old' master has already been push-ed to another repository such as GitHub. How does this change things?

推荐答案

另外两个答案的问题是新的master没有老的master作为祖先,所以当你push的时候,其他人都会得到弄乱.这就是你想要做的:

The problem with the other two answers is that the new master doesn't have the old master as an ancestor, so when you push it, everyone else will get messed up. This is what you want to do:

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

如果您希望您的历史记录更清晰一点,我建议您在合并提交消息中添加一些信息,以明确您所做的事情.将第二行改为:

If you want your history to be a little clearer, I'd recommend adding some information to the merge commit message to make it clear what you've done. Change the second line to:

git merge --strategy=ours --no-commit master
git commit          # add information to the template merge message

这篇关于将当前 Git 分支设为 master 分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆