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


当前回答

git rm  -r ./* --cached

这将删除所有缓存/暂存文件

其他回答

从git 2.23开始,您可以简单地使用以下命令:

git restore --staged <file>

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

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

我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从上一次提交移回临时区域的问题,而不取消对它们所做的更改。这可以像Pariosh Singh建议的那样:

git reset --soft HEAD^ 

or

git reset --soft HEAD~1

然后重置不需要的文件,以便将它们从提交中删除(旧方法):

git reset HEAD path/to/unwanted_file

注意,自从Git 2.23.0以来,人们可以(以新的方式):

git restore --staged path/to/unwanted_file

现在再次提交,您甚至可以重复使用相同的提交消息:

git commit -c ORIG_HEAD  

以下内容将显示您想要的文件,这是OP要求的。

git reset HEAD^ /path/to/file

您将看到以下内容。。。

要提交的更改:(使用“git reset HEAD…”取消页面)修改:/path/to/file未提交的更改:(使用“git add…”更新将提交什么)(使用“gitcheckout--…”放弃工作目录中的更改)修改:/path/to/file

“要提交的更改”是提交之前文件的先前版本。如果文件从未存在,这看起来像是删除。如果您提交此更改,则会有一个修订将更改还原为分支中的文件。“未提交的更改”是您提交的更改,以及文件的当前状态

此时,您可以对文件执行任意操作,例如重置为不同的版本。

当您准备好承诺时:

git commit --amend -a

或者(如果您还有其他不想提交的更改)

git commit add /path/to/file
git commit --amend
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)