使用 git 恢复提交的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4795600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverting part of a commit with git
提问by skiphoppy
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.
我想在 git 中恢复一个特定的提交。不幸的是,我们的组织仍然使用 CVS 作为标准,所以当我提交回 CVS 时,多个 git 提交被合并为一个。在这种情况下,我很想挑出原始的 git 提交,但这是不可能的。
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?
是否有类似的方法git add --patch
可以让我有选择地编辑差异以决定要还原提交的哪些部分?
回答by mipadi
Use the --no-commit
(-n
) option to git revert
, then unstage the changes, then use git add --patch
:
使用--no-commit
( -n
) 选项git revert
,然后取消暂存更改,然后使用git add --patch
:
$ git revert -n $bad_commit # Revert the commit, but don't commit the changes
$ git reset HEAD . # Unstage the changes
$ git add --patch . # Add whatever changes you want
$ git commit # Commit those changes
Note: The files you add using git add --patch are the files you want to revert, not the files you want to keep.
注意:您使用 git add --patch 添加的文件是您要还原的文件,而不是您要保留的文件。
回答by user1338062
I have used the following successfully.
我已经成功使用了以下内容。
First revert the full commit (puts it in index) but don't commit.
首先还原完整提交(将其放入索引中)但不要提交。
git revert -n <sha1> # -n is short for --no-commit
Then interactively remove the reverted GOOD changes from the index
然后以交互方式从索引中删除恢复的 GOOD 更改
git reset -p # -p is short for --patch
Then commit reverse diff of the bad changes
然后提交错误更改的反向差异
git commit -m "Partially revert <sha1>..."
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
最后,恢复的 GOOD 更改(已通过 reset 命令取消暂存)仍在工作树中。他们需要清理。如果工作树中没有其他未提交的更改,则可以通过
git reset --hard
回答by jeffcook2150
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.
就我个人而言,我更喜欢这个版本,它重用自动生成的提交消息,并让用户有机会在最终提交之前编辑和粘贴“部分”一词。
# generate a revert commit
# note the hash printed to console on success
git revert --no-edit <hash to revert>
# undo that commit, but not its changes to the working tree
# (reset index to commit-before-last; that is, one graph entry up from HEAD)
git reset HEAD~1
# interactively add reversions
git add -p
# commit with pre-filled message
git commit -c <hash from revert commit, printed to console after first command>
# reset the rest of the current directory's working tree to match git
# this will reapply the excluded parts of the reversion to the working tree
# you may need to change the paths to be checked out
# be careful not to accidentally overwrite unsaved work
git checkout -- .
回答by Krzysztof Kaczmarski
Solution:
解决方案:
git revert --no-commit <commit hash>
git reset -p # every time choose 'y' if you want keep the change, otherwise choose 'n'
git commit -m "Revert ..."
git checkout -- . # Don't forget to use it.
回答by Walf
Another alternative (if your current version of a file isn't too far from the version you're trying to revert) is to get the hash of the commit immediately beforethe one you wish to partially revert (from git log
). Then your command becomes:
另一种选择(如果您当前的文件版本与您尝试还原的版本相差不远)是在您希望部分还原(从git log
)之前立即获取提交的哈希值。然后你的命令变成:
$ git checkout -p <hash_preceding_commit_to_revert> -- file/you/want/to/fix.ext
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
.
这确实会更改您的工作树中的文件,但不会创建任何提交,因此,如果您真的搞砸了,您可以重新开始git reset --hard -- file/you/want/to/fix.ext
.
回答by William Pursell
You can use git-revert -n, and then use add --patch to select hunks.
您可以使用 git-revert -n,然后使用 add --patch 来选择大块头。