英文:
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 <hash-id>
(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 <hash-id>
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分支内容相同?
按照上述方法将实现你的目标,然而如果“内容相同”也意味着dev
和master
应该指向相同的提交,那么你对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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论