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.

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.


Reset to head:

To hard reset a single file to HEAD:

git checkout @ -- myfile.ext

Note that @ is short for HEAD .

Reset to index:

To hard reset a single file to the index, assuming the index is non-empty, otherwise to HEAD:

git checkout -- myfile.ext

The point is that to be safe, you don't want to leave out @ or HEAD from the command unless you specifically mean to reset to the index only.


you can use the below command for reset of single file

git checkout HEAD -- path_to_file/file_name

List all changed files to get path_to_file/filename with below command

git status
链接地址: http://www.djcxy.com/p/8982.html

上一篇: 从git中的旧提交恢复文件

下一篇: 硬重置单个文件