如何完全忽略已推送的提交并与主分支合并?

huangapple go评论65阅读模式
英文:

How to completely ignore pushed commits and merged with master branch?

问题

dev 分支上推送了一些提交,并将它们合并到 master 分支后,我想回到之前的第四个提交。可以使用 git reset --hard <hash-id>(其中 hash-id 是第四个之前的提交)来做到这一点。

但当我想再次将其推送到 dev 分支时,它会显示“先执行 git pull”,因为远程 dev 分支上存在新的更改。

为了解决这个问题,我运行了以下命令:

git reset --hard <hash-id>
git clean -d -f
git push -f origin dev

现在 dev 分支是我想要的了,但当我想将其与 master 分支合并时,它显示“找不到任何更改”。

这很奇怪,因为在 dev 分支和 master 分支中,代码库是不同的,但它却说找不到任何更改。我该如何使 dev 分支和 master 分支具有相同的内容?

英文:

After pushing some commits on the dev branch, and merging them with master branch. I want to back to 4 commits ago. I can do that using git reset --hard &lt;hash-id&gt; (which hash-id is the 4th previous commits)

but when I want to push it again on the dev branch, it says "do a git pull first" because news changes exits on the remote dev branch.

To fix the problem, I run the following commands:

git reset --hard &lt;hash-id&gt;
git clean -d -f
git push -f origin dev

Now the dev branch is what I want, but when I want to merge it with the master branch it says "no changes found".

It is very strange, because the code base is different in branch dev and master, but it says that no changes were found. How can I make the dev branch and master the same content?

答案1

得分: 1

这是发生的原因,因为你无法使用合并来移除提交,而你已经将那4个曾经在dev分支上的提交合并到了master分支。如果你想让它们从master分支中也消失,你需要对master分支进行硬重置,回退到在dev分支合并之前的状态,然后也需要强制推送master分支。(这假设你允许强制推送master分支;有关另一种方法,请参见下面的附注。)

当将dev分支合并到master分支时,你看到“未找到更改”的原因是Git查看提交ID以确定哪些在dev分支上的提交ID不在master分支上。无论你将dev分支重置回1个提交还是100个提交,你仍会得到相同的消息,因为在dev分支上没有任何新的提交不在master分支上。

附注: 一个“撤销”那4个提交的替代方法,而不是像你所做的那样向后重置,是使用revert。如果将dev分支合并到master分支创建了一个合并提交(merge commit),那么你可以简单地在master分支上撤销该合并提交,它将撤销那4个提交带来的更改。是否应该重置还是撤销通常取决于分支是否是共享的。如果其他人正在使用master分支,那么重置和强制推送可能会对他们造成干扰,因此推荐使用撤销。

另一个附注: 你还提到过:

> 如何使dev分支和master分支内容相同?

按照上述方法将实现你的目标,然而如果“内容相同”也意味着devmaster应该指向相同的提交,那么你对master的重置命令可以简单地是:

git switch master # 切换到master分支
git reset --hard dev # 将master指向dev所指向的提交
git push --force-with-lease # 这比单纯的 --force 更安全
英文:

This is happening because you cannot remove commits using merge, and you already merged those 4 commits that used to be on dev into master. If you want them gone from master also you'll need to do a hard reset of master to before the merge of dev, and then force push master as well. (This assumes you are OK with force pushing master; see the Side Note below for an alternative.)

The reason you see "no changes found" when merging dev into master, is because Git looks at the commit IDs to see which commit IDs are on dev that are not on master. Whether you reset dev back 1 commit, or 100 commits you'd still get that same message because there aren't any new commits on dev that aren't already on master.

Side Note: An alternative way to "undo" those 4 commits, instead of resetting backwards like you did, is to use revert. If the merge of dev into master created a merge commit on master, then you can simply revert that merge commit on master and it will undo the changes brought in by those 4 commits. Whether you should reset or revert is usually dependent on whether the branch is shared or not. If other people are using the master branch then resetting and force pushing it could mess them up, so revert would be preferred.

Another Side Note: You also mentioned:

> How can I make the dev branch and master the same content?

Doing as mentioned above will achieve your goal, however if by "same content" you also mean that dev and master should point to the same commit, then your reset command of master could simply be:

git switch master # checkout master
git reset --hard dev # point master to what dev is pointing to
git push --force-with-lease # this is safer than just --force

huangapple
  • 本文由 发表于 2023年4月10日 20:18:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定