撤消上次提交/合并

发布于 2024-10-31 17:45:43 字数 241 浏览 6 评论 0原文

我把我的 git 仓库弄乱了一点。我在一个单独的分支中开发了一个功能。完成工作后,我切换到master将其合并到其中,但我的伙伴推送了一些与我的冲突的文件。在合并、冲突和推动新的更改之后,我发现我也提交了合作伙伴的旧更改。

现在我想重做这个提交/合并。我试过 git reset --soft HEAD^ 但是当我想推送时,我收到此错误消息再次推送之前合并远程更改

谁能帮助我吗?

I have messed up my git repo a little. I worked on a feature in a separate branch. After finishing the work, I switched to the master to merge it into, but my partner pushed a few files which came into conflict with mine. After the merge, the conflict and pushing the new changes, I saw that I committed the older changes of my partner too.

Now I want to redo this commit/merge. I tried
git reset --soft HEAD^ but when I wanted to push, I got this error message Merge the remote changes before pushing again.

Can anyone help me?

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

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

发布评论

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

评论(5

救赎№ 2024-11-07 17:45:43

你的功能分支仍然会指向你的工作。您不会丢失这些更改。

正如plaes所说,您可以将master重置回一个

git reset --hard HEAD^

如果您想从分支中获取一些特定文件而不合并,您可以检查它们:

git checkout yourbranch -- file1 file2 etc

如果您想要合并之前从master中获取一些文件,您也可以检查这些:

git checkout master^ -- file3 file4 etc

这并不理想,但有时这是需要的。合并/可能/意味着您拒绝合并中任何一方的一些更改。实现正确合并的最佳方法是:

git merge --no-commit yourbranch

从 master 开始,然后从上面运行 git checkout 命令,最后提交:

git add . -A
git commit

当您现在推送此分支时,您需要添加 force 选项

git push --force

git push -f

Hope这有帮助。

Your feature branch will still point to your work. You will not lose those changes.

As plaes said, you can reset master back one with

git reset --hard HEAD^

If you want to grab some specific files from your branch without merging, you can check them out:

git checkout yourbranch -- file1 file2 etc

If you want some files from master from before the merge you can also check these out:

git checkout master^ -- file3 file4 etc

This is not ideal but it is what is needed sometimes. A merge /may/ mean that you reject some changes from either side in a merge. The best way to attain a proper merge is to:

git merge --no-commit yourbranch

from master, then run the git checkout commands from above and finally commit:

git add . -A
git commit

When you push this branch now, you will need to add the force option

git push --force

or

git push -f

Hope this helps.

红颜悴 2024-11-07 17:45:43

呵呵,你差不多明白了:

git reset --hard HEAD^

Hehe, you almost figured it out:

git reset --hard HEAD^
小红帽 2024-11-07 17:45:43

这里的问题是您想要撤消已经推送到中央存储库的更改。如果你一开始就没有推送它们,那么 git reset --hard/--soft/--mixed HEAD^ 就可以解决问题。

因此,当您重置 HEAD 时,当您尝试推送到原点并更新不是 HEAD 祖先的远程引用时,git 会抱怨。动武:

git push --force origin master

The problem here is that you want to undo changes that you already have pushed to the central repository. If you hadn't pushed them in the first place, a git reset --hard/--soft/--mixed HEAD^ would have done the trick.

So when you have reseted your HEAD git complains when you try to push to the origin and update the remote ref which is not an ancestor to your HEAD. Use --force:

git push --force origin master
梦境 2024-11-07 17:45:43

使用 git reset --hard origin/master 代替,这将进行硬重置并删除合并。取自链接 撤消尚未推送的 Git 合并

Use git reset --hard origin/master instead, this will do hard reset as well as delete the merge as well. Take from link Undo a Git merge that hasn't been pushed yet

慢慢从新开始 2024-11-07 17:45:43

让我为像我这样不太了解 git 的用户简化一下:D

如果你想取消合并提交并将其从远程 git 中删除。按照:

git reset --hard HEAD^

或者

git reset --merge hash-of-commit-before-merge i.e. c4ab5de

现在您已在本地成功重置,是时候在远程更新/同步了

git push --force // this will remove the merge or afterwards commits

Let me simplify it for the users that don't understand git very well, like me :D

If you want to unmerge the commit and remove it from the remote git as well. Follow:

git reset --hard HEAD^

or

git reset --merge hash-of-commit-before-merge i.e. c4ab5de

Now you have successfully reset on your local, it's time to update/sync this on remote

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