如何从最新提交中删除文件?


当前回答

最好的方法是使用SourceTree(也可以根据需要从命令行执行),并且:

检查新分支机构(出于安全原因)然后在要更改的提交之前使用gitreset-soft进行提交它将允许您删除(暂存/取消暂存)不需要的文件:)

其他回答

现在,苹果已经将所有MacOS用户更新为zsh,正确的答案不再有效。

git reset --soft HEAD^
zsh: no matches found: HEAD^

您可以通过单引号防止shell将^视为特殊字符。

git reset --soft 'HEAD^'

或者,您可以在shell中禁用此行为。通过更新~/.zshrc

unsetopt nomatch
Here is the step to remove files from Git Commit.

>git reset --soft HEAD^1(either commitid ) -- now files moved to the staging area.
>git rm --cached filename(it will removed the file from staging area)
>git commit -m 'meaningfull message'(Now commit the required files)

目前的答案都不合理。听起来有足够的需求,应该提出一个真正的解决方案:https://github.com/git/git/blob/master/Documentation/SubmittingPatches

git--取消提交<文件名>

很好。我知道我们不想修改历史记录,但如果我是本地人,无意中添加了本地“黑客”文件,并希望将其从提交中删除,这将非常有用。

如果您正在使用GitHub,但尚未提交,GitHub Desktop可以轻松解决此问题:

选择存储库->撤消最近提交取消选择错误添加的文件。您的上一条提交消息将已经出现在对话框中。按下提交按钮!

git checkout HEAD~ path/to/file
git commit --amend