Git - 从历史记录中删除提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30893040/
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
Git - Remove commit from history
提问by Serban Stoenescu
I did something extremely stupid. I entered a curseword in my code and I pushed the code on the master branch. I pushed a few more times after that so people do not pull the bad stuff, but I can still find the curseword in the commits history.
我做了一件非常愚蠢的事情。我在代码中输入了一个诅咒词,然后将代码推送到主分支上。在那之后我又推了几次,这样人们就不会拉坏东西,但我仍然可以在提交历史中找到诅咒词。
Is there a way to delete the commit from history? Edit: I don't want to add the file to gitignore because we need that file.
有没有办法从历史记录中删除提交?编辑:我不想将该文件添加到 gitignore,因为我们需要该文件。
Thanks
谢谢
采纳答案by Maciej Ozi?b?y
If it's only on your local PC (or noone checked out your changes):
如果它仅在您的本地 PC 上(或没有人检查您的更改):
1) Use:
1) 用途:
git log
混帐日志
to find the commit you want to remove. Copy hash (the long sqeuence like: e8348ebe553102018c...).
找到要删除的提交。复制哈希(长序列如:e8348ebe553102018c...)。
2) Use:
2) 用途:
git rebase -i your_commit_hash_code_comes_here~
git rebase -i your_commit_hash_code_comes_here~
Just remove the commit you don't need and save the file.
只需删除不需要的提交并保存文件。
Interactive git rebase can let you also fix the broken commit - there is no need to remove it.
交互式 git rebase 还可以让您修复损坏的提交 - 无需删除它。
If you pushed changes to the server or someone already got your changes - never change history - it'd cause serious problems for your team.
如果您将更改推送到服务器或有人已经获得了您的更改 - 永远不要更改历史记录 - 这会给您的团队带来严重的问题。
回答by David Deutsch
Once you push to the repo, you really don't want to go about changing history. However, if you are absolutely surethat nobody has pulled/fetched from the repo since your offending commit, you have 2 options.
一旦你推送到 repo,你真的不想去改变历史。但是,如果您绝对确定自您的违规提交以来没有人从 repo 中提取/获取,您有 2 个选择。
If you want to remove the commit altogether (and every commit that came after that), do a git reset --hard ABC~
(assuming the commit's hash is ABC
). Then do a git push -f
.
如果您想完全删除提交(以及之后的每个提交),请执行git reset --hard ABC~
(假设提交的哈希为ABC
)。然后做一个git push -f
.
If you just want to edit that commit, and preserve the commits that came after it, do a git rebase -i ABC~
. This will launch your editor, showing the list of your commits, starting with the offending one. Change the flag from "pick" to "e", save the file and close the editor. Then make the necessary changes to the files, and do a git commit -a --amend
, then do git rebase --continue
. Follow it all up with a git push -f
.
如果您只想编辑该提交,并保留其后的提交,请执行git rebase -i ABC~
. 这将启动您的编辑器,显示您的提交列表,从有问题的提交开始。将标志从“pick”更改为“e”,保存文件并关闭编辑器。然后对文件进行必要的更改git commit -a --amend
,然后执行a ,然后执行git rebase --continue
. 用一个git push -f
.
I want to repeat, these options are only available to you if nobodyhas done a pull or fetch that contains your offending commit. If they have, doing these steps will just make matters worse.
我想重复一遍,这些选项只有在没有人执行过包含您的违规提交的拉取或提取时才对您可用。如果他们有,执行这些步骤只会让事情变得更糟。
回答by NutCracker
This works for me:
这对我有用:
git log
to find the commit you want to remove and copy its hashgit rebase -i <commit_hash>~
which opens your text editor- in text editor, switch from
pick
todrop
for your particular commit
git log
找到要删除的提交并复制其哈希git rebase -i <commit_hash>~
这将打开您的文本编辑器- 在文本编辑器中,从 切换
pick
到drop
您的特定提交