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


当前回答

事实上,我认为一种更快更简单的方法是使用git rebase交互模式。

git rebase -i head~1  

(或头~4,你想走多远)

然后,使用“edit”代替“pick”。我没有意识到“编辑”有多强大。

https://www.youtube.com/watch?v=2dQosJaLN18

希望你会发现它有帮助。

其他回答

git checkout HEAD~ path/to/file
git commit --amend
git rm  -r ./* --cached

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

使用rm删除文件将删除它!

您总是在git中添加提交,而不是删除,因此在本例中,将文件返回到第一次提交之前的状态(如果文件是新的,这可能是删除“rm”操作),然后重新提交,文件将继续。

要将文件恢复到以前的状态,请执行以下操作:

    git checkout <commit_id> <path_to_file>

或者将其返回到远程HEAD的状态:

    git checkout origin/master <path_to_file>

然后修改提交,您应该会发现文件已从列表中消失(并且没有从磁盘中删除!)

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--取消提交<文件名>

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