恢复推送的 git 提交

发布于 2024-10-22 00:02:06 字数 596 浏览 6 评论 0原文

我有一个有两个分支的存储库——master 和 dev。我正在主分支上工作并拉取,并收到一条消息说存储库是最新的。我提交了更改,并推送到远程存储库(在 github 上)。我收到一条消息,说某些更改被拒绝。

然后我做了一个 git pull origin dev ,这显然是错误的做法——因为它将 dev 分支与我的 master 合并了,就像一个白痴一样,我没有注意到这一点,直到我已经又推了。所以最后一次提交显示了合并 github.com:myuser/myrepo 的分支“dev”

我可以通过执行 git reset --hard [sha] 回到本地存储库上最后一个已知的良好状态,其中 [sha] 是合并之前的提交(尽管我不确定然后如何对原点进行更改)——或者根据我读到的内容,我也可以执行 git revert -m ,然后提交/推送该更改。

任何人都可以引导我通过“正确的方式”撤消合并,并将两个分支恢复到合并之前的位置吗?

谢谢——如果重要的话,这是一个只有两个开发人员的共享存储库,所以它不会发生重大变化。

编辑添加:请像我是个孩子一样跟我说话。我不得不承认 Git 的东西仍然让我感到困惑,所以我距离高级用户还很远!谢谢

I've got a repo with two branches-- master and dev. I was working on the master branch and pulled, and got a message that the repo was up to date. I committed my changes, and pushed to the remote repo (on github). I got a message saying that some changes were rejected.

I then did a git pull origin dev, which apparently was the wrong thing to do-- since it merged the dev branch with my master, and like an idiot I didn't notice this until I'd already pushed again. So the last commit shows Merge branch 'dev' of github.com:myuser/myrepo.

I can get back to the last known good status on my local repo by doing a git reset --hard [sha], with [sha] being the commit before the merge (though I'm not sure how to then make that change to the origin)-- or from what I've read I can also do a git revert -m and then commit/push that change.

Can anyone walk me through the "right way" to undo my merge, and restore both branches back to where they were prior to the merge?

Thanks-- if it matters this is a shared repo with only two developers, so it's not under heavy changes.

Edit to add: please talk to me as if I were a child. I have to admit this Git stuff still confuses me, so I'm far from a power user! Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-10-29 00:02:06

git reset --hard [sha] 将更正本地存储库上的分支。要强制此推送工作,您可以执行git push origin +master:master。 + 号将强制非线性推动起作用。

如果其他开发人员已经拉取了您错误的提交,他们将必须执行 git 远程更新,然后执行 git reset --hard origin/master (前提是他们是在他们的 master 分支上,并且没有进行任何其他提交,

请谨慎使用这些命令:-)。祝你好运。

The git reset --hard [sha] will correct the branch on your local repository. To force this push to work, you can do a git push origin +master:master. The + sign will force the non-linear push to work.

If the other developers have already pulled you wrong commit, they will have to do a git remote update, then a git reset --hard origin/master (provided that they are on their master branch, and have not made any other commits.

Please use these commands with some caution :-). Good luck.

怎言笑 2024-10-29 00:02:06

FelipeFG 的答案在两个开发人员的环境中工作得很好,您可以轻松地协调重做其他人的本地存储库,但实际上您最好使用 git revert -m<主线分支的父 id,在本例中为 1>; <提交引用>。然后,当你完成修复它时,只需 git revertgit merge dev (在这种情况下恢复你的恢复很重要,因为否则git merge dev 不会将您的旧合并视为祖先,这将导致必须解决的冲突)。

历史会很丑陋,但它也会支持快进,而且你不用担心一些可怜的闷棍会因为你的历史修改而解开一团糟的局部冲突。

请参阅 http: //opensource.apple.com/source/Git/Git-26/src/git-htmldocs/howto/revert-a-faulty-merge.txt 了解详细信息。

FelipeFG's answer works fine in a two-developer context where you can easily coordinate redoing the other guy's local repository, but you're actually better off using git revert -m<parent id of mainline branch, 1 in this case> <commit ref>. Then, when you're finished fixing it, just git revert <revert commit>, and git merge dev (it's important to revert your revert for this case, because otherwise git merge dev will not consider your old merge an ancestor, and this will lead to conflicts that have to be resolved).

The history will be ugly, but it will also support fast-forwarding, and you won't have to worry about some poor sap untangling a mess of local conflicts thanks to your history revision.

See http://opensource.apple.com/source/Git/Git-26/src/git-htmldocs/howto/revert-a-faulty-merge.txt for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文