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


当前回答

将文件保存在本地(从gitcommit中删除)

如果要保留文件,可以使用--cached,如下所示:

git rm --cached filename

删除本地文件(并从gitcommit中删除)

否则,如果要删除文件,只需使用:

git rm filename

其他回答

一些对我有用的东西,但仍然认为应该有更好的解决方案:

$ git revert <commit_id>
$ git reset HEAD~1 --hard

只需将要放弃的更改保留在另一个提交中,检查其他人

$ git commit --amend // or stash and rebase to <commit_id> to amend changes

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

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

git reset --soft HEAD~1

它将重置所有更改并恢复为一次提交

如果这是您最后一次提交,并且您希望从本地和远程存储库中删除文件,请尝试以下操作:

git rm <file>
 git commit --amend

甚至更好:

先重置

git reset --soft HEAD~1

重置不需要的文件

git reset HEAD path/to/unwanted_file

再次提交

git commit -c ORIG_HEAD  

将文件保存在本地(从gitcommit中删除)

如果要保留文件,可以使用--cached,如下所示:

git rm --cached filename

删除本地文件(并从gitcommit中删除)

否则,如果要删除文件,只需使用:

git rm filename

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)