Importing History into Git
With my recent move over to Git for my VCS Home solution I decided to start afresh with new repositories and not migrating the history over, in the last few days i’ve noted that it was a very bad decision and having the full history will always be useful. Now i’m stuck in the situation of two repos with different histories. How do you reconcile these two trees into one full history tree?
First of all we need a working Git version of your existing repo, in my case, it’s in SVN, so I used the git-svn tool to import my svn repository.
$ cd ~/dev/ $ git svn clone http://tensixtyone.com/svn/home/trunk/bash
Git then downloads each SVN commit and imports it, this can be quite slow on large repositories but thankfully mine was only 20 or so commits. Now you have your originally repository in Git format the few final steps will bring in the changes you have done in the new repository.
To import the history we are going to generate a set of diffs, while this sounds less than ideal it is really the only clean way to get your new commits into your old repository, attempting to pull the commits in will throw errors as the commit hashes will not match.
First of all you need to find out the hash of your first commit in your new repository
$ cd ~/.dotfiles/bash
$ git log
...
commit ec508803a080f2146231fb4cd396cc18a2906a9b
Author: Andrew Williams
Date: Sat Apr 11 02:32:07 2009 +0100
Imported initial bash files
Then generate the diffs since that initial commit
$ git format-patch ec508803a080f2146231fb4cd396cc18a2906a9b..HEAD 0001-Added-bash_logout-file.patch 0002-Updated-bash-config-files.patch 0003-Updated-a-few-aliases.patch 0004-Updated-prompt-to-detect-if-we-re-using-vcsh.patch 0005-Fixed-nano-alias.patch
Now you have your fresh diffs, you need to import them into your existing repository. In addition i’m importing these into a new branch.
$ cd ~/dev/bash/ $ git branch new-bash $ git am ~/*.patch
The am command is mostly used to apply patches from a mailbox but for this case we can just tell it to use the patch files instead. After the command has completed you should be able to check git log and be able to see all the new commits.
Once your happy with the patches, it’s a case of switching to master and merging the changes.
$ git checkout master $ git merge new-bash
One thing to note, after this process the commits will have different hashes, so it wont be a simple case of pushing to your remote repo. I’d only recommend doing this if you have to, i’m sure that a better proceedure exists but this is what worked for me.
