如何从一开始就重新建立所有提交的基础

所以我正在从 svn (code.google.com)迁移到 git (github)。

我已经从 svn repo 导入了我的项目,它沿途导入了所有的提交历史。 我对这些评论并不感到自豪,因为这是我的第一个项目之一,并不是很严肃。

我希望将所有内容重新设置为一个“初始导入”提交。

我把电脑里的东西都调出来了,我正试着这么做,但是我发现: git rebase -i master,但它只重新基于新的修改和提交。

如何使用 rebase 清除所有历史记录中的 github 存储库?

39358 次浏览

If you'd like to reduce all your history to a single "Initial import" commit, simply remove .git directory and create a new local repository (keeping a backup of the old one). git init . && git add . && git commit -m "Initial import".

Such new repository won't have a common ancestor with the one you've pushed to GitHub, so you'll have to git push --force your newly created repository.

Find the hash of the commit that you want to start squashing from say abcd12 and then rebase against that hash specifically.

git rebase -i abcd12

You are using master to rebase against, which performs the rebase against the tip of the master branch.

You could rebase and squash everything if you wanted to (except the initial commit) but why bother? Simply delete your .git directory, run git init to recreate it, git add everything, and git commit to make a new initial commit.

Jefromi's answer will work, but if you want to keep your existing repo configuration, or even leave around the commits just in case, you could do the following:

git checkout master

git branch backup optionally leave another branch here in case you want to keep your history.

git reset --soft $SHA_OF_INIT_COMMIT this will update what HEAD is pointing to but leave your index and working directory in their current state. You can get the SHA with git log --pretty=format:%h --reverse | head -n 1, and make this one step with git reset --soft $(git log --pretty=format:%h --reverse | head -n 1)

git commit --amend change your initial commit to point to the current state of your repo.

git rebase -i --root will start an interactive rebase of all commits from the beginning.

From there, you can squash all commits into one and/or perform other edits.