如何回滚Git仓库首先提交并删除所有历史记录 [英] How to roll back Git repo to first commit and delete all history
问题描述
我不知道有什么方法可以完全满足您的要求可以回滚到第一次提交,但不能删除所有历史记录,因为历史将至少包含该初始提交。)
如果我是你我只是删除远程repo和本地仓库的 .git
目录,并重新开始 git init
。
我能得到的最接近的内容是除第一次提交之外的所有内容。要做到这一点,你首先要找到第一次提交的SHA1,例如:
%git rev-list - max-parents = 0 --abbrev-commit HEAD
aa8119f
...然后运行
%git重置aa8119f
...或
%git reset --hard aa8119f
code>
...取决于您是要保留还是放弃自该初始提交以来所做的所有更改。 (上面假设你只有一个分支,如果没有,你还必须删除你有 git branch -d< BRANCHNAME>
的任何其他分支。)
最后,您可以运行
%git push -f
(我希望你认识到 不幸的是,正如已经解释的那样,这种方法不会删除 all 历史记录。 如果这是您经常要做的事情,我建议您立即在 这会在您的历史记录的根部放置一个空的提交,并使用名为ROOT的标记对其进行标记。然后你可以做一些类似于 或 将您带回第一个空提交。 到好好处理 I'm learning how to use git these days and I had to do many hit-and-misses. Thus I needed to delete and create anew my remote and local repos. Is there a way to roll back to the first commit of the repo and delete all history after that? Basically a clean slate to experiment on. I don't know of any way to do exactly what you're asking (one can roll back to first commit, but not delete all history, since the history will at least contain that initial commit.) If I were you I'd just delete the remote repo and the The closest that I can get to what you're asking for would be to rollback all but the first commit. To do that, you'd first find the SHA1 of the first commit, for example: ...and then run either ...or ...depending on whether you want to preserve or discard all the changes made since that initial commit. (The above assumes that you have only one branch. If not, you'll also have to delete any other branches you have with Finally, you'd run (I hope that you realize that Unfortunately, as already explained, this approach does not delete all the history. If this is something you'll want to do often, I'd recommend that, immediately after This will put an empty commit at the root of your history, and tag it with a tag named ROOT. Then you can do something like or to bring you back to that first empty commit. To get a good handle on what 这篇关于如何回滚Git仓库首先提交并删除所有历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! git push -f $ c
git init
,你运行类似于
%git commit --allow-empty --allow -empty-message -m''
%git tag -a -m''ROOT
%git reset ROOT
%git reset --hard ROOT
git reset
的操作,我建议阅读这个。.git
directory of the local repo, and start over with git init
.% git rev-list --max-parents=0 --abbrev-commit HEAD
aa8119f
% git reset aa8119f
% git reset --hard aa8119f
git branch -d <BRANCHNAME>
.)% git push -f
git push -f
is a no-no whenever you're pushing to a repo that is shared with others.)git init
, you run something like% git commit --allow-empty --allow-empty-message -m ''
% git tag -a -m '' ROOT
% git reset ROOT
% git reset --hard ROOT
git reset
does, I recommend reading this.