删除 git 中一个文件的更改

Remove changes from one file in git

例如,我从一个干净的提交开始,对不同的文件做了一些更改,然后我想删除我只对一个文件所做的更改。我该怎么做?
我尝试这样做:

git stash -p train.py

但是我遇到了错误:

fatal: subcommand wasn't specified; 'push' can't be assumed due to unexpected token 'train.py'

你应该:

git rebase HEAD~1

# Change, by the editor just opened, the word pick into edit, save and exit
# Edit you file

git commit

您收到该错误的原因是因为 git stash pushgit stash -p 是其别名)不允许自定义参数以避免意外存储。

来自 git stash push 的文档:

For quickly making a snapshot, you can omit "push". In this mode, non-option arguments are not allowed to prevent a misspelled subcommand from making an unwanted stash entry. The two exceptions to this are stash -p which acts as alias for stash push -p and pathspec elements, which are allowed after a double hyphen -- for disambiguation.

在您的情况下,train.pypathspec,因此您必须通过添加 -- 将其与识别的选项分开,如下所示:

git stash -p -- train.py

或者,您可以拼出整个命令,在这种情况下,您不必用双破折号分隔路径:

git stash push -p train.py

请注意,使用双破折号 -- 可以清楚地分隔所有接受路径的 Git 命令使用的 pathspec from a command's known options is a convention