Remove an unreferenced commit from git repository

落花浮王杯 提交于 2019-11-30 15:31:35

问题


I have a git commit history like this:

          U
         / 
A---B---C---D---E master

Nothing points to the commit U, but I know its hash. How can I completely remove this commit from my repository as if it never existed? I'm the only person using this repo.

I tried using git rebase, but that can either delete parts of a branch or move commits, but doesn't seem to be able to delete a single commit.

If I do git checkout <hash> and then git reset --hard HEAD~1 I don't see the the commit anymore. Is it actually gone completely or is it still hidden in the repo?


回答1:


Eventually it will be cleaned up by git, but you can look into git gc to actually force a garbage collect.

The clean command is different and won't take care of this for you.




回答2:


As described here, simply use

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

git reflog expire --expire-unreachable=now --all removes all references of unreachable commits in reflog.

git gc --prune=now removes the commits themselves.

Attention: Only using git gc --prune=now will not work since those commits are still referenced in the reflog. Therefore, clearing the reflog is mandatory.



来源:https://stackoverflow.com/questions/13812103/remove-an-unreferenced-commit-from-git-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!