Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

git - 单个文件的硬重置(Hard reset of a single file)

I currently have three modified files in my working directory.

(我目前在工作目录中有三个修改过的文件。)

However I want one of them to be reset to the HEAD status.

(但是我希望其中一个重置为HEAD状态。)

In SVN I'd use svn revert <filename> (followed by svn update <filename> if needed) but in git I should use git reset --hard .

(在SVN中,我使用svn revert <filename> (如果需要,然后是svn update <filename> )但是在git中我应该使用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?

(有没有办法在git中丢弃单个文件更改并用新的HEAD副本覆盖它?)

  ask by Emiliano translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(...将使用HEAD更新my-file.txt的工作副本及其在索引中的状态。)

-- basically means: treat every argument after this point as a file name .

(--基本上意味着: 将此点之后的每个参数视为文件名 。)

More details in this answer .

(这个答案的更多细节。)

Thanks to VonC for pointing this out.

(感谢VonC指出这一点。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...