git revert --no-commit 没有暂存

标签 git git-revert

通常 git revert 命令会自动创建一些带有提交日志消息的提交,说明哪些提交已被还原。

要避免自动提交,可以选择 -n(或 --no-commit)。

但是在这个命令之后,还原的文件在暂存区。我可以使用命令 git reset HEAD 取消暂存它们。

有没有无需提交和暂存的直接还原提交的方法?

换句话说:是否有直接命令来恢复提交,仅将更改应用于工作目录,而不触及索引?

最佳答案

没有针对它的单一命令。正如您已经注意到的,您可以将 git revert -ngit reset 结合使用以在索引中撤消还原。

除此方法外,您还可以使用 git apply -R 来“反向应用”补丁,并且可以使用 git show 将提交变成补丁,因此:

$ git show <rev> | git apply -R

具有相同的效果,但有一个重要(但微妙)的区别。让我引用 git revert 文档并添加我自己的重点:

-n, --no-commit
Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.

换句话说,git revert -n 在索引中和之上操作,具有更新工作树的副作用。 git apply 命令仅在工作树上运行(无论如何默认情况下),实际上可以在 git 存储库之外使用。

关于git revert --no-commit 没有暂存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33674426/

相关文章:

Git:处理子分支

Git 恢复某些文件

Git 恢复失败

git - 撤消 git pull --rebase

git - 删除推送的提交并回滚到特定提交

git - 使用 Git 管理网站开发的建议?

git - 在 Visual Studio 2010 beta 2 中使用 Git

ruby - Git 用 CRLF 替换了我所有的 LF - 我该如何解决这个问题?

git - 如何使用git查看另一个开发者的分支?

git revert merge 策略问题(他们的)