如何回滚Git仓库首先提交并删除所有历史记录 [英] How to roll back Git repo to first commit and delete all history

查看:832
本文介绍了如何回滚Git仓库首先提交并删除所有历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习如何使用git,而且我不得不做很多次的命中和失误。因此我需要删除并重新创建我的远程和本地回购。有没有办法回滚到回购协议的第一次提交并在此之后删除所有历史记录?基本上是一个干净的石板来试验。

解决方案

我不知道有什么方法可以完全满足您的要求可以回滚到第一次提交,但不能删除所有历史记录,因为历史将至少包含该初始提交。)



如果我是你我只是删除远程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 

(我希望你认识到 git push -f

不幸的是,正如已经解释的那样,这种方法不会删除 all 历史记录。



如果这是您经常要做的事情,我建议您立即在 git init ,你运行类似于

 %git commit --allow-empty --allow -empty-message -m''
%git tag -a -m''ROOT

这会在您的历史记录的根部放置一个空的提交,并使用名为ROOT的标记对其进行标记。然后你可以做一些类似于

 %git reset ROOT 

 %git reset --hard ROOT 

将您带回第一个空提交。



到好好处理 git reset 的操作,我建议阅读这个


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 .git directory of the local repo, and start over with git init.

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:

% git rev-list --max-parents=0 --abbrev-commit HEAD
aa8119f

...and then run either

% git reset aa8119f

...or

% git reset --hard aa8119f

...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 git branch -d <BRANCHNAME>.)

Finally, you'd run

% git push -f

(I hope that you realize that git push -f is a no-no whenever you're pushing to a repo that is shared with others.)

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 git init, you run something like

% git commit --allow-empty --allow-empty-message -m ''
% git tag -a -m '' ROOT

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

% git reset ROOT

or

% git reset --hard ROOT

to bring you back to that first empty commit.

To get a good handle on what git reset does, I recommend reading this.

这篇关于如何回滚Git仓库首先提交并删除所有历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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