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


当前回答

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 reset --soft HEAD^
git checkout origin/master <filepath>

干杯

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

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

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

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

我们可以采取的另一种方法是:

删除文件使用删除的文件进行新提交执行交互式重基并压缩两个提交推

我将用例子向你解释。设A、B、C为3次连续提交。提交B包含一个不应提交的文件。

git log  # take A commit_id
git rebase -i "A_commit_ID" # do an interactive rebase
change commit to 'e' in rebase vim # means commit will be edited
git rm unwanted_file
git rebase --continue
git push --force-with-lease <branchName>    

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

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

git gui citool --amend

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

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