在最后一次提交后,我修改了工作副本中的一堆文件,但我想撤消对其中一个文件的更改,如将其重置为与最近一次提交相同的状态。
然而,我只想撤消仅对该文件的工作副本更改,而不做其他更改。
我该怎么做?
在最后一次提交后,我修改了工作副本中的一堆文件,但我想撤消对其中一个文件的更改,如将其重置为与最近一次提交相同的状态。
然而,我只想撤消仅对该文件的工作副本更改,而不做其他更改。
我该怎么做?
当前回答
对我来说,只有这个有效
git checkout -p filename
其他回答
git checkout <commit> <filename>
我今天使用这个是因为我意识到我的收藏夹在几次提交之前被覆盖了,当我升级到drupal 6.10时,所以我不得不把它拿回来。以下是我所做的:
git checkout 088ecd favicon.ico
如果您的文件已经暂存(在编辑文件后执行gitadd等操作时发生),则取消对更改的暂存。
Use
git reset HEAD <file>
然后
git checkout <file>
如果尚未暂存,请使用
git checkout <file>
如果您尚未推送或以其他方式共享您的承诺:
git diff --stat HEAD^...HEAD | \
fgrep filename_snippet_to_revert | cut -d' ' -f2 | xargs git checkout HEAD^ --
git commit -a --amend
对我来说,只有这个有效
git checkout -p filename
您可以使用
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