简体   繁体   English

如何从 Git 中的最新提交中删除文件或还原提交?

[英]How to delete a file from the most recent commit in Git or revert the commit?

I would like to delete some files from a specific commit or revert the commit in Git. I accidentally added some files to my commit and I want to discard them before pushing my project to my remote repository.我想从特定提交中删除一些文件或还原 Git 中的提交。我不小心将一些文件添加到我的提交中,我想在将我的项目推送到我的远程存储库之前丢弃它们。

I've been trying with git revert but it didn't work out.我一直在尝试使用git revert ,但没有成功。

If necessary (because of the revert) you might need to reset --hard back to the troublesome commit (thus erasing the revert commit).如有必要(因为还原),您可能需要reset --hard回到麻烦的提交(从而擦除还原提交)。 That, if needed, would be如果需要,那将是

git reset --hard @~1

You are now at the bad commit.你现在处于错误的提交状态。 Now simply现在简单

git reset @~1

You are now back where you were before forming the bad commit.您现在回到了形成错误提交之前的位置。 You are ready to make the commit again, but correctly this time!您已准备好再次提交,但这次是正确的! Examine the situation with git status .查看状态为git status Add files but not the ones you regret adding, and commit.添加文件但不添加您后悔添加的文件,然后提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM