I recently had cause to try and merge two branches from separate git repositories that had related content but disconnected ancestry. The situation was caused by creating a git repository from a CVS conversion. I then wanted to join work from a separate git repository that had been started from the same CVS tree. However, the developer who made this git repository had begun his git tree having already made changes to the content.
So I ran git cvsimport to convert all the CVS history into one git repository. I can now pull in the new work by adding another git repository as a new remote and fetching the master branch from this remote. However, what I now have is a disconnected chain of commits. There are now two separate chains in my repository. What I need to do is merge the two together. I can do this if I can take the first commit of the new work and inject it into the cvs converted repository as the next commit. I don't want to apply a patch, I need the state of the repository for this commit to be exactly that of the first commit of the new work. For this there is git read-tree. This command takes a git tree and reads it into the index. This is where git stages your work when you do git add and git rm and where it assembles the new tree to be committed by git commit. So by setting the index to a given tree, we are setting up a for a new commit that is exactly the tree provided. Just what I need. Having staged this tree, I can go ahead and commit it and then cherry-pick or merge the rest of the tree. In my case I cherry-picked so that I could fix the author name and tidy up the commit messages.
Below are the commands I used to fix up the tile-qt repository. First adding the remote and fetching its commits. Checking the correct tree and loading it into the index and then updating the working files from the index.
git remote add jdc file:///opt/src/tile-qt-jdc git fetch jdc git ls-tree 1519481 git read-tree -m 1519481 git checkout-index -f -u -a
The final results can then be pushed to some public location like github or in this case the tktable sourceforge project.