如何从已推送的 git commit 中删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32212783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove a file from a git commit that has been pushed
提问by ianus
I mistakenly used git commit -a and added more files than I wanted to in a commit. There was another modified file that I wanted to add as a separate commit included in this commit. How do I go back ( without losing the modifications to the files ) and then commit them all again separately.
我错误地使用了 git commit -a 并在提交中添加了比我想要的更多的文件。我想添加另一个修改过的文件作为此提交中包含的单独提交。我如何返回(不丢失对文件的修改)然后分别再次提交它们。
I've also pushed this to a remote origin ( though I know for a fact that no one has pulled anything since I pushed).
我也把它推到了一个远程源(尽管我知道自从我推以来没有人拉过任何东西)。
Many Thanks!
非常感谢!
回答by Sam
Warning, this will overwrite the changes remotely. If anyone has taken down the current commit and worked on top of it, bad things could happen.
警告,这将远程覆盖更改。如果有人删除了当前的提交并在其之上工作,那么可能会发生不好的事情。
# Reset last commit, but keep changes staged
$ git reset --soft HEAD^1
# Unstage unwanted file(s) and recommit
$ git reset HEAD path/to/file
$ git commit
# Push the new commit with force to overwrite changes
$ git push origin -f
回答by RafalskyCom
You can just remove these files locally and push a new commit.
您可以在本地删除这些文件并推送新的提交。
If you like to excude these files in future you can add it to .gitignore file
如果您以后想排除这些文件,可以将其添加到 .gitignore 文件中
回答by Courtney Faulkner
I would immediately push the previous commit to the remote, run git reset HEAD~
to put your workspace to a dirty state you were in, and then reprocess your adding, committing, and pushing.
我会立即将之前的提交推送到远程,运行git reset HEAD~
以将您的工作区置于您所处的脏状态,然后重新处理您的添加、提交和推送。
回答by GolezTrol
If you pushed it to a separate branch (not the master), you can delete the whole branch from the remote.
如果你把它推送到一个单独的分支(不是主分支),你可以从远程删除整个分支。
After that, you can revert the commit using
之后,您可以使用还原提交
git reset --soft HEAD~1
Then you can start committing and pushing everything again.
然后你可以开始提交并再次推送所有内容。