How to rebase all the commits from the beginning

GitGithub

Git Problem Overview


So I'm migrating from svn (code.google.com) to git(github).

I've imported my project from the svn repo, and it imported all the commit history along way. I'm not really proud of those comments as it was one of my first project, not really serious.

I'd like to rebase everything into a single 'initial import' commit.

I've pulled everything on my computer and I'm trying to do that. But all I found was: git rebase -i master but it only rebases new modifications and commits.

How can I clean my github repository from all history with a rebase?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionplus-View Question on Stackoverflow
Solution 1 - GitTobias JView Answer on Stackoverflow
Solution 2 - GitCascabelView Answer on Stackoverflow
Solution 3 - GitKevin M GrangerView Answer on Stackoverflow
Solution 4 - GitAbizernView Answer on Stackoverflow
Solution 5 - GitJanView Answer on Stackoverflow