Git undo推送合并并删除其历史记录 [英] Git undo pushed merge and delete its history

查看:79
本文介绍了Git undo推送合并并删除其历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意外合并到master并推送了它,现在master拥有了dev的所有提交.我想在不更改dev的情况下从master还原提交并删除其历史记录.我该怎么办?

I made an accidental merge to master and pushed it, now master has all the commits from dev. I want to revert the commits from master and delete its history without changing dev. How can I do that?

推荐答案

可以使用git reset --hard HEAD^删除上一次提交.

The last commit can be removed with: git reset --hard HEAD^.

有时可能需要从分支的中间"删除提交的情况.这是救援交互基础:git rebase -i <commit>^.您只需要删除一个不需要的提交(它应该出现在列表的顶部).

Sometimes there might be situations, when you need to remove a commit from the "middle" of branch. Here comes to the rescue interactive rebase: git rebase -i <commit>^. You just need to drop an unwanted commit (it should appear at the top of the list).

如果您撤消的更改在远程可用,则也应通过以下方法从那里将其删除:git push --force.

If the changes you undone were available in remote, they also should be removed from there with force push: git push --force.

但是,重写已经与某人共享的分支的历史记录并不是撤消更改的好方法.相反,请考虑使用git revert <commit>.在这种情况下,这是更健壮和正确的方法.

However, rewriting the history of a branch, that has been shared with someone, is not a good way of undoing changes. Instead, consider to use git revert <commit>. This is the more robust and correct way in this situation.

我建议阅读 Git分支-重新设置. 重建基础的危险"一章解释了为什么重写公共历史不是一个好主意.

I'd recommend to read Git Branching - Rebasing. Chapter "The Perils of Rebase" is explaining why rewriting the public history is not a good idea.

这篇关于Git undo推送合并并删除其历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆