:::info 相关的两个问题:
- https://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-github
- https://stackoverflow.com/questions/9683279/make-the-current-commit-the-only-initial-commit-in-a-git-repository
:::
问题背景
我有一个Gitee上的仓库,但是偶然间错误的commit了一些内容,虽然我最后又更新了一版,但是之前的commit内容却还在git存储库中保存着记录,洁癖有些受不了,就google了下,找到了不错的方法。解决策略
参考了这条回答:https://stackoverflow.com/a/13102849The only solution that works for me (and keeps submodules working) is
git checkout --orphan newBranch
git add -A # Add all files and commit them
git commit
git branch -D master # Deletes the master branch
git branch -m master # Rename the current branch to master
git push -f origin master # Force push master branch to github
git gc --aggressive --prune=all # remove the old files
Deleting
.git/
always causes huge issues when I have submodules. Usinggit rebase --root
would somehow cause conflicts for me (and take long since I had a lot of history).
关于最后一点,评论区里有回答到:
Git will keep the old files around for a while, to get rid of them run
git gc --aggressive --prune=all
.
需要注意
这里的 git add -A
的理解可以看这里https://www.cnblogs.com/e0yu/p/8608674.html,看上去在2.x版本中,与 git add .
无异。