问题描述
我做了一个提交,然后用
I did a commit and reverted with
git revert HEAD^
只是git log
➜ git:(master) git log
commit 45a0b1371e4705c4f875141232d7a97351f0ed8b
Author: Daniel Palacio <[email protected]>
Date: Tue Jan 17 16:32:15 2012 -0800
Production explanation
但是如果我做git log的话 - 它仍然显示出来。我需要将它从历史记录中删除,因为它有敏感信息
But if I do git log --all it still show up. I need to remove it from the history as it has sensitive information
git log --all
commit 5d44355080500ee6518f157c084f519da47b9391
Author: Daniel Palacio
Date: Tue Jan 17 16:40:48 2012 -0800
This commit has to be reset
commit 45a0b1371e4705c4f875141232d7a97351f0ed8b
Author: Daniel Palacio
Date: Tue Jan 17 16:32:15 2012 -0800
Production explanation
如何从历史记录中删除提交5d44355080500ee6518f157c084f519da47b9391?
How do I remove the commit 5d44355080500ee6518f157c084f519da47b9391 from the history too?
推荐答案
, git revert
在这里是错误的命令。这将创建一个新的提交,恢复较旧的提交。这不是你要求的。其次,它看起来像是要恢复 HEAD
而不是 HEAD ^
。
First off, git revert
is the wrong command here. That creates a new commit that reverts an older one. That's not what you're asking for. Secondly, it looks like you want to revert HEAD
instead of HEAD^
.
如果你没有把它推到任何地方,你可以使用 git reset --hard HEAD ^
来丢弃最新的提交(这也会抛弃任何未提交的更改,因此请确保您没有任何要保存的内容)。假设你对你的副本和其他人的敏感信息没有问题,那么你就完成了。你可以继续工作,后续的 git push
不会推送你的错误提交。
If you haven't pushed this anywhere, you can use git reset --hard HEAD^
to throw away the latest commit (this also throws away any uncommitted changes, so be sure you don't have any you want to save). Assuming you're ok with the sensitive information being present in your copy and nobody else's, you're done. You can continue to work and a subsequent git push
won't push your bad commit.
如果这不是安全的假设(尽管如果不是,我很想听听为什么),那么你需要过期你的reflogs,并强制垃圾收集,现在收集所有未完成的对象。你可以这样做:
If that's not a safe assumption (though if not I'd love to hear why), then you need to expire your reflogs and force a garbage collection that collects all outstanding objects right now. You can do that with
git reflog expire --expire=now --expire-unreachable=now --all
git gc --prune=now
虽然这只能在你确实需要做它。
though this should only be done if you really absolutely need to do it.
如果你有推动你的提交,那么你几乎没有运气。您可以执行强制推送来远程恢复它(尽管只有在远程端允许的情况下才可以),但是您不能从远程端的数据库中删除提交本身,所以任何有权访问该存储库的人都可以找到它知道要寻找什么。
If you have pushed your commit, then you're pretty much out of luck. You can do a force-push to revert it remotely (though only if the remote side allows that), but you can't delete the commit itself from the remote side's database, so anyone who has access to that repository can find it if they know what to look for.
这篇关于Git恢复上次提交并将其从历史记录中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!