简体   繁体   English

从git历史记录中删除提交(永远)

[英]remove commits (forever) from git history

I would like to permanently remove specified commits from the entire history of a git repository. 我想从git存储库的整个历史记录中永久删除指定的提交。 I saw this thread , in which the following is recommended: 我看到了这个线程 ,在其中推荐以下内容:

git reset --hard HEAD~4
git push origin HEAD --force

That's fine and resets the status of my repository, but in no way does it remove my commits. 很好,可以重置存储库的状态,但绝不会删除我的提交。 I would like to literally roll back history and discard changes to the repo since 4 revisions ago. 我想从字面上回滚历史记录,并放弃自4个修订版以来对仓库的更改。

Could anyone help me with this? 有人可以帮我吗? When I try the above and look at the revision history in Github, I still see unwanted commits sitting there. 当我尝试上述内容并查看Github中的修订历史记录时,仍然看到坐在那里的不需要的提交。

I saw this article , but I wanted to check whether or not there were other options before investigating this solution. 我看到了这篇文章 ,但是在研究此解决方案之前,我想检查是否还有其他选择。

From your question, what actually happens when you run 根据您的问题,当您跑步时实际会发生什么

git push origin HEAD --force

is unclear, but I can think of at least two reasons why it may fail to force-push master to origin : 尚不清楚,但我可以想到至少两个原因,为什么它可能无法将master强制推向origin

  1. the currently checked out branch is not master and is a branch already up-to-date on origin , or 当前已签出的分支不是 master分支,并且该分支已经是origin上的最新分支,或者
  2. you have a detached HEAD . 您有一个独立的HEAD

Make sure master is indeed the currently checked-ou branch, by running git checkout master ; 通过运行git checkout master确保master确实是当前签出的分支。 then run your force push command. 然后运行您的强制推送命令。 Alternatively, specify master explicitly in the git push command: 或者,在git push命令中显式指定master

git push --force origin master

That should do it. 那应该做。

If the unwanted commits are not referenced anymore by a branch or a tag (ie called "loose objects"), then they will be automatically removed by the garbage collector. 如果不需要的提交不再被分支或标签(即所谓的“松散对象”)引用,则垃圾收集器将自动删除它们。 By default, the GC acts on objects older than 90 days. 默认情况下,GC作用于90天以上的对象。

You can still manually trigger an earlier GC with: 您仍然可以通过以下方式手动触发较早的GC:

git reflog expire --expire=now --all
git gc --prune=all

However, that will act only on your local repository, not on GitHub. 但是,这只会在您的本地存储库上起作用,而不会在GitHub上起作用。 GitHub runs a weekly GC with its own options so if you want to remove the commits now , then you will have to delete the repository, recreate it and push your local after GC cleanup. GitHub每周运行一次带有自己选择的GC,因此,如果您现在要删除提交,则必须删除存储库,重新创建存储库,并在GC清理后将其推送到本地。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM