如何回滚 Git 存储库以首先提交并删除所有历史记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16499908/
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
How to roll back Git repo to first commit and delete all history
提问by Aleksandar Savkov
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.
这些天我正在学习如何使用 git,我不得不做很多事。因此,我需要删除并重新创建我的远程和本地存储库。有没有办法回滚到 repo 的第一次提交并在此之后删除所有历史记录?基本上是一个全新的实验。
回答by kjo
I don't know of any way to do exactly what you're asking (one can roll back to first commit, but not delete allhistory, 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
.
如果我是你,我会删除远程仓库和.git
本地仓库的目录,然后从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:
我能得到的最接近您要求的是回滚除第一次提交之外的所有内容。为此,您首先要找到第一次提交的 SHA1,例如:
% 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>
.)
...取决于您是要保留还是放弃自初始提交以来所做的所有更改。(以上假设您只有一个分支。如果没有,您还必须删除您拥有的任何其他分支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.)
(我希望您意识到,git push -f
每当您推送与他人共享的存储库时,这是一个禁忌。)
Unfortunately, as already explained, this approach does not delete allthe history.
不幸的是,正如已经解释过的,这种方法不会删除所有历史记录。
If this is something you'll want to do often, I'd recommend that, immediately after git init
, you run something like
如果这是您经常想要做的事情,我建议git init
您在之后立即运行类似
% 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
这将在历史记录的根目录放置一个空提交,并使用名为 ROOT 的标签对其进行标记。然后你可以做类似的事情
% 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 reset
,我建议阅读这篇。
回答by GoZoner
Just blow away the .git
directory once you've got your first commit checked out. As such:
.git
签出第一次提交后,只需将目录吹走即可。像这样:
git checkout <first-commit-sha>
rm -rf .git
git init
git add -A
git commit -m 'Initial Commit'
回答by VonC
You could reset to the first commit:
您可以重置为第一次提交:
"How to show first commit by 'git log'?" describes how to find the first commit:
“如何通过 'git log' 显示第一次提交?”描述了如何找到第一次提交:
git log --pretty=format:%H | tail -1
(works only if there is no multiple root branches)
(仅当没有多个根分支时才有效)
git reset --hard yourFirstCommitSHA1
Note that after the reset, to reallyget a clean slate, you could simply git init
a new repo and copy the content of your first commit you just reset to (and add and commit in that new repo)
请注意,在重置之后,要真正获得一个干净的石板,您可以简单地创建git init
一个新的存储库并复制您刚刚重置的第一次提交的内容(并在该新存储库中添加和提交)
回答by jthill
To do a clean reset you want to eliminate the logs and any config changes and everything, I'd do this by fetching just the master root into a new repo:
要进行干净的重置,您希望消除日志和任何配置更改以及所有内容,我会通过将主根目录提取到新存储库中来完成此操作:
git tag master-root $(git rev-list --topo-order master|sed '$!d')
git init ../reset-my-repo
cd ../reset-my-repo
git fetch $OLDPWD master-root
git checkout -B master FETCH_HEAD
(i.e. pretty much what VonC said)
(即几乎 VonC 所说的)
(added --topo-order
to protect against bad timestamps)
(添加--topo-order
以防止错误的时间戳)
回答by mvp
You can certainly remove all history in current branch using:
您当然可以使用以下方法删除当前分支中的所有历史记录:
git reset --hard <commit_id>
where commit_id
is sha1 of some historic commit.
commit_id
一些历史性提交的 sha1在哪里。
However, it is rarely needed if you are learning and want to experiment.
但是,如果您正在学习并想要进行实验,则很少需要它。
Instead, you can simply create new branch for your experiments, like:
相反,您可以简单地为您的实验创建新分支,例如:
git branch experiment <commit_id>
git checkout experiment
...
This would be identical for most intents and purposes to first variant, but you can switch back if you want.
对于大多数意图和目的,这与第一个变体相同,但您可以根据需要切换回去。
You can can also rename branches, such that your experiments have original branch names, like:
您还可以重命名分支,以便您的实验具有原始分支名称,例如:
git branch -m master backup
git branch master
git checkout master
If you want to nuke backup
branch, simply do:
如果你想核backup
分支,只需执行以下操作:
git branch -D backup
回答by Alexey Slusar
Checkout to master:
结帐大师:
$ git checkout master
Show log:
显示日志:
$ git log
Reset to first commit:
重置为第一次提交:
$ git reset --hard <you first commit number>
Then, push changes to remote:
然后,将更改推送到远程:
$ git push --force origin