git 如何撤消 repo 的合并分支 'master' 到开发分支

git how to undo Merge branch 'master' of repo into develop branch

我正在使用开发分支并进行了提交。

然后,我本来应该做一个 git pull origin develop - 但我不小心做了这个 git pull origin master

然后,我看到了这条消息Merge branch 'master' of repo into develop branch

有什么方法可以撤销吗?

重置合并提交:

git reset --hard HEAD~

再拉

git pull origin develop

PS:我假设您的 develop 分支实际指向合并提交。

编辑

if I do this, would I loose the commit made in develop branch ? No, right ?

提交将不再被引用,但它仍然存在。 Git 将保留一段时间无法访问的提交。默认值为 90 天。参见 git gc

The optional configuration variable gc.reflogExpire can be set to indicate how long historical entries within each branch’s reflog should remain available in this repository. The setting is expressed as a length of time, for example 90 days or 3 months. It defaults to 90 days.

所以在这段时间内你可以恢复commit,但是你必须知道它的id。您可以在 reflog 中找到该 ID(如果它尚未过期或将其保存在某个地方。

如果您对此不满意,可以在执行 git reset --hard 之前创建一个指向 develop 分支实际提交的本地分支。在这种情况下,本地备份分支将引用提交并且 git gc 不会删除它(因为它被引用)。例如

git branch backup/develop
git reset --hard HEAD~

如果您现在认为:"Oh no I want to go back"。就这样

git reset --hard backup/develop

否则删除本地备份分支

git branch -D backup/develop