使用 git 恢复部分提交
我想恢复 git 中的特定提交。不幸的是,我们的组织仍然使用 CVS 作为标准,因此当我重新提交到 CVS 时,多个 git 提交会合并为一个。在这种情况下,我很想挑出原始的 git 提交,但这是不可能的。
是否有一种类似于 git add --patch 的方法可以让我有选择地编辑差异来决定要恢复提交的哪些部分?
I want to revert a particular commit in git. Unfortunately, our organization still uses CVS as a standard, so when I commit back to CVS multiple git commits are rolled into one. In this case I would love to single out the original git commit, but that is impossible.
Is there an approach similar to git add --patch
that would allow me to selectively edit diffs to decide which parts of a commit to revert?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
--no-commit
(-n
) 选项进行git revert
,然后取消暂存更改,然后使用git add - -patch
:注意:使用 git add --patch 添加的文件是要恢复的文件,而不是要保留的文件。
Use the
--no-commit
(-n
) option togit revert
, then unstage the changes, then usegit add --patch
:Note: The files you add using git add --patch are the files you want to revert, not the files you want to keep.
我已经成功地使用了以下内容。
首先恢复完整提交(将其放入索引)但不提交。
然后以交互方式从索引中删除恢复的良好更改
然后提交不良更改的反向差异
最后恢复的良好更改(已被重置命令取消暂存)仍然在工作树中。它们需要清理。如果工作树中没有留下其他未提交的更改,则可以通过以下方式完成
I have used the following successfully.
First revert the full commit (puts it in index) but don't commit.
Then interactively remove the reverted GOOD changes from the index
Then commit reverse diff of the bad changes
Finally the reverted GOOD changes (which have been unstaged by the reset command) are still in the working tree. They need to be cleaned up. If no other uncommitted changes are left in the working tree, this can be done by
解决方案:
Solution:
另一种选择(如果文件的当前版本与您尝试恢复的版本相差不太远)是交互地
签出
。查看 git log 并获取您想要撤消的提交的哈希值,或者紧邻其之前的提交的哈希值。您的命令变为:(其中
^
表示父提交)或:然后,您可以在修补时通过选择
s
(拆分)选项来选择要还原的块或其中的一部分。这确实会更改工作树中的文件,但不会创建任何提交,因此,如果您真的搞砸了,可以使用 git reset --hard -- file/you/want/to/fix.ext 重新开始。
Another alternative (if your current version of a file isn't too far from the version you're trying to revert) is to
checkout
interactively. Look atgit log
and get the hash of the commit you wish to undo, or the one immediately before it. Your command becomes either:(where
^
means parent commit of) or:You can then choose hunks to revert, or parts thereof by choosing the
s
(split) option when patching.This does change the files in your working tree but creates no commits, so if you really stuff up you can just start again with
git reset --hard -- file/you/want/to/fix.ext
.就我个人而言,我更喜欢这个版本,它重用自动生成的提交消息,并让用户有机会在最终提交之前编辑并粘贴“部分”一词。
Personally, I prefer this version, which reuses the auto-generated commit message and gives the user the opportunity to edit and stick the word "Partially" in before finally committing.
您可以使用 git-revert -n,然后使用 add --patch 来选择帅哥。
You can use git-revert -n, and then use add --patch to select hunks.