git 如何恢复上次提交并将其从历史记录中删除?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8903953/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:45:59  来源:igfitidea点击:

How to revert last commit and remove it from history?

gitgit-revert

提问by daniel

I did a commit and reverted with

我做了一个提交并恢复了

git revert HEAD^

just git log

只是 git 日志

?  git:(master) git log
commit 45a0b1371e4705c4f875141232d7a97351f0ed8b
Author: Daniel Palacio <[email protected]>
Date:   Tue Jan 17 16:32:15 2012 -0800

    Production explanation

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 它仍然会出现。我需要从历史记录中删除它,因为它包含敏感信息

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

How do I remove the commit 5d44355080500ee6518f157c084f519da47b9391 from the history too?

如何从历史记录中删除提交 5d44355080500ee6518f157c084f519da47b9391?

回答by Lily Ballard

First off, git revertis 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 HEADinstead of HEAD^.

首先,git revert这里是错误的命令。这会创建一个新的提交来恢复旧的提交。那不是你要的。其次,看起来您想要恢复HEAD而不是HEAD^.

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 copyand nobody else's, you're done. You can continue to work and a subsequent git pushwon't push your bad commit.

如果您没有将它推送到任何地方,您可以使用git reset --hard HEAD^扔掉最新的提交(这也会扔掉任何未提交的更改,因此请确保您没有要保存的任何更改)。假设您对副本中存在的敏感信息感到满意,而其他人没有,那么您就完成了。您可以继续工作,后续git push不会推动您的错误提交。

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

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

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 havepushed 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.

如果你已经推动了你的提交,那么你就很不走运了。您可以执行强制推送以远程恢复它(尽管只有在远程端允许的情况下),但您不能从远程端的数据库中删除提交本身,因此任何有权访问该存储库的人都可以找到它,如果他们知道要寻找什么。

回答by manojlds

If you don't care about the commit, just do:

如果您不关心提交,请执行以下操作:

git reset --hard HEAD~

to blow away the commit.

吹走提交。

If you want the changes to be in working directory, do:

如果您希望更改位于工作目录中,请执行以下操作:

git reset HEAD~

Depending on what you have done with git revert, you might have to change the above commands. Revert creates a new commit that reverts the commit you wanted to revert. So there will be two commits. You might have to do HEAD~2to remove them both.

根据您对 所做的工作git revert,您可能需要更改上述命令。Revert 创建一个新的提交,用于还原您想要还原的提交。所以会有两次提交。您可能必须HEAD~2删除它们。

Note that, usually, revert is the safer way to, well, revert changes. But here, since you want to remove sensitive data, reset is the best approach.

请注意,通常,还原是还原更改的更安全的方法。但是在这里,由于您要删除敏感数据,因此重置是最好的方法。

回答by dyrssen

There is a nice solution here. To delete the last (top) commit you can do

有一个很好的解决方案在这里。要删除最后一个(顶部)提交,您可以执行

git push [remote] +[bad_commit]^:[branch]

where [bad_commit] is the commit that [branch] currently points to, or if the [branch] is checked out locally, you can also do

其中 [bad_commit] 是 [branch] 当前指向的提交,或者如果 [branch] 在本地签出,您也可以这样做

git reset HEAD^ --hard
git push [remote] -f

回答by Bruno Oliveira

If you have not pushed the commit yet, you can just:

如果你还没有推送提交,你可以:

git reset --hard HEAD~2

git reset --hard HEAD~2

(HEAD~2 to remove your original commit and your "revert" commit).

(HEAD~2 删除您的原始提交和“还原”提交)。

This will reset your current branch to the point in history before the commit you want to remove. If that commit is not in any other branch, it will not be pushed to your origin.

这会将您当前的分支重置为您要删除的提交之前的历史点。如果该提交不在任何其他分支中,则不会将其推送到您的源。

回答by chenchuk

Here is a simple working solution that delete the last commit from remote:

这是一个简单的工作解决方案,可以从远程删除最后一次提交:

  1. clone the repo and find the last 'good' commit (....c407)
  1. 克隆 repo 并找到最后一个“好”提交(....c407)
$ git clone git@host:PROJ/myrepo.git 
$ cd myrepo 
$ git log --pretty=oneline

234987fed789de97232ababababcef3234958723 bad_commit
e4a2ec4a80ef63e1e2e694051d9eb3951b14c407 v4.3
2f116449144dbe46e7260d5dac2304803751b5c2 v4.2
$ git clone git@host:PROJ/myrepo.git 
$ cd myrepo 
$ git log --pretty=oneline

234987fed789de97232ababababcef3234958723 bad_commit
e4a2ec4a80ef63e1e2e694051d9eb3951b14c407 v4.3
2f116449144dbe46e7260d5dac2304803751b5c2 v4.2
  1. checkout the last good commit to a new temp branch
  1. 签出对新临时分支的最后一次良好提交
$ git checkout e4a2ec4a80ef63e1e2e694051d9eb3951b14c407
$ git checkout -b temp_branch
$ git checkout e4a2ec4a80ef63e1e2e694051d9eb3951b14c407
$ git checkout -b temp_branch
  1. replace the remote branch ( by delete and push the temp )
  1. 替换远程分支(通过删除并推送 temp )
git push origin --delete dev_branch
git push origin temp_branch:dev_branch
git push origin --delete dev_branch
git push origin temp_branch:dev_branch