117

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?

2

5 Answers 5

291

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, this saves my life! I wanted to keep the small commits messages to polish the final initial commit and this works out of the box. I'm glad that I kept scrolling.
For extra bonus points. git rebase -i --signoff -S --root. This will both signoff and sign the commits with your user, which is something at least I missed the first time, but would have wanted afterwards, and should have done while I was rewriting all history. Now I did it a second time :D
21

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.

2 Comments

Actually - yours is a better answer for just bundling a single commit. I'll leave mine up just to show that you can specify a commit to start rebasing from.
Note removing .git also removes your hooks and other configuration if you have one.
11

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.

3 Comments

As pointed out in this answer, you should not be using git log for scripting. You're better off with git rev-list --max-parents=0 HEAD - but I still like your answer as the best one. :) The logic being: 1) Move master to point to 1st commit, 2) change that commit to hold what's in my working directory right now, 3) voila! new initial commit with all changes squashed neatly together :)
@JohnySkovdal that answer says to instead use "git-log with specified custom format", which I'm doing, no?
Darn, somehow missed that part. Read it as git log was no good no matter the parameters. Nvm me then. :$
5

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.

4 Comments

it worked for the history, but the message next to each file still shows the commit message from before.
Did you actually squash the files? it gives an option to create a new commit message.
Yeah I did squash everything but the first commit. I've removed all the commit messages. I was seeing it ok in github history, but not on individual files. Maybe it was a cache issue from github though. Anyway thanks, I've ended up initializing a new repo.
This is certainly the right answer if you don't want to squash all the history! Unfortunately due to the way Git thinks, you can't actually squash it all into the initial commit; you'll be left with the initial one and one squashed one after it. (It's also slower, since it has to individually apply each commit.)
3

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.

1 Comment

You don't need to remove .git. Just create an orphan branch.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.