在最后一次提交后,我修改了工作副本中的一堆文件,但我想撤消对其中一个文件的更改,如将其重置为与最近一次提交相同的状态。

然而,我只想撤消仅对该文件的工作副本更改,而不做其他更改。

我该怎么做?


当前回答

您可以使用

git checkout -- file

您可以不使用--(如nimrodm所建议的),但如果文件名看起来像分支或标记(或其他修订标识符),则可能会混淆,因此最好使用--。

您还可以检出文件的特定版本:

git checkout v1.2.3 -- file         # tag v1.2.3
git checkout stable -- file         # stable branch
git checkout origin/master -- file  # upstream master
git checkout HEAD -- file           # the version from the most recent commit
git checkout HEAD^ -- file          # the version before the most recent commit

其他回答

如果您想撤消对该文件的上一次提交更改,可以尝试以下操作:

git checkout branchname^ filename

这将按上次提交前的状态签出文件。如果您想再提交几次,请使用branchname~n表示法。

git checkout a3156ae4913a0226caa62d8627e0e9589b33d04c -p */SearchMaster.jsp

细分:a3156ae4913a0226caa62d8627e0e9589b33d04c=这是提交的哈希值。这是我自己的分支(而不是主分支)。

-p标志是路径。

*/SearchMaster.jsp是文件名。

如果您的文件已经暂存(在编辑文件后执行gitadd等操作时发生),则取消对更改的暂存。

Use

git reset HEAD <file>

然后

git checkout <file>

如果尚未暂存,请使用

git checkout <file>

我通过gitbash完成了:

(使用“gitcheckout--<file>…”放弃工作目录中的更改)

Git状态。[所以我们看到一个文件被修改了。]git checkout--index.html[我已在index.html文件中更改:git状态[现在这些更改已删除]

您可以使用

git checkout -- file

您可以不使用--(如nimrodm所建议的),但如果文件名看起来像分支或标记(或其他修订标识符),则可能会混淆,因此最好使用--。

您还可以检出文件的特定版本:

git checkout v1.2.3 -- file         # tag v1.2.3
git checkout stable -- file         # stable branch
git checkout origin/master -- file  # upstream master
git checkout HEAD -- file           # the version from the most recent commit
git checkout HEAD^ -- file          # the version before the most recent commit