从 Git 提交中删除文件 [英] Remove files from Git commit

查看:33
本文介绍了从 Git 提交中删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Git 并且我已经使用

I am using Git and I have committed few files using

git commit -a

后来我发现一个文件被错误地添加到了提交中.

Later, I found that a file had mistakenly been added to the commit.

如何从上次提交中删除文件?

How can I remove a file from the last commit?

推荐答案

我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从前一次提交移回暂存区的问题,而不取消更改对他们做了.这可以像 Paritosh Singh 建议的那样完成:

I think other answers here are wrong, because this is a question of moving the mistakenly committed files back to the staging area from the previous commit, without cancelling the changes done to them. This can be done like Paritosh Singh suggested:

git reset --soft HEAD^ 

git reset --soft HEAD~1

然后重置不需要的文件,以便将它们从提交中排除:

Then reset the unwanted files in order to leave them out from the commit:

git reset HEAD path/to/unwanted_file

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

Now commit again, you can even re-use the same commit message:

git commit -c ORIG_HEAD  

这篇关于从 Git 提交中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆