如何还原上一次提交并将其从历史记录中删除? [英] How to revert last commit and remove it from history?

查看:107
本文介绍了如何还原上一次提交并将其从历史记录中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一次提交,并用

git revert HEAD^

只是git log

➜  git:(master) git log
commit 45a0b1371e4705c4f875141232d7a97351f0ed8b
Author: Daniel Palacio <danpal@gmail.com>
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.

如果这不是一个安全的假设(尽管我不想听到为什么),那么您需要使您的reflog过期并强制执行垃圾收集,以立即收集所有未完成的对象.您可以使用

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.

这篇关于如何还原上一次提交并将其从历史记录中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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