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


当前回答

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)

其他回答

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

使用gitGUI可以简化从先前提交中删除文件的过程。

假设这不是一个共享分支,并且您不介意重写历史,那么运行:

git gui citool --amend

您可以取消检查错误提交的文件,然后单击“提交”。

该文件将从提交中删除,但将保留在磁盘上。因此,如果在错误添加文件后取消选中该文件,它将显示在未跟踪文件列表中(如果在错误修改文件后取消检查该文件,则会显示在未提交的更改列表中)。

如果要从以前的提交中删除文件,请使用筛选器

git filter-branch --prune-empty --index-filter 'git rm --ignore-unmatch --cached "file_to_be_removed.dmg"'

如果看到此错误:

无法创建新备份。refs/original中已存在以前的备份/强制使用-f覆盖备份

只需删除本地存储库上的refs备份

$ rm -rf .git/refs/original/refs

如果你不再需要那个文件,你可以

git rm file
git commit --amend
git push origin branch

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

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

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