问题描述
我目前在我的工作目录中有三个修改过的文件。然而,我希望其中的一个被重置为HEAD状态。
在SVN中,我会使用 svn revert< filename>
(后跟 svn update< filename>
如果需要的话),但在git中,我应该使用 git reset --hard
。然而,这个命令不能在单个文件上运行。
有没有什么办法可以放弃单个文件的更改并用新的HEAD副本覆盖它?
您可以使用以下命令:
git checkout HEAD - my-file.txt
...这将更新工作 my-file.txt
的副本及其在索引中的状态与HEAD中的状态。
-
基本上意味着:将这个点之后的每个参数视为文件名称。 中的更多详细信息。感谢指出了这一点。
I currently have three modified files in my working directory. However I want one of them to be reset to the HEAD status.
In SVN I'd use svn revert <filename>
(followed by svn update <filename>
if needed) but in git I should use git reset --hard
. However this command cannot operate on a single file.
Is there any way in git to discard a single file changes and overwrite it with a fresh HEAD copy?
You can use the following command:
git checkout HEAD -- my-file.txt
... which will update both the working copy of my-file.txt
and its state in the index with that from HEAD.
--
basically means: treat every argument after this point as a file name. More details in this answer. Thanks to VonC for pointing this out.
这篇关于硬重置单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!