Easy one-time git subtree merge

The situation:

  • Have a git project (A)
  • Have a second git project (B) that I want to merge to A under a directory
  • This needs to be done once. After that, project B will not be re-sync to A’s subdirectory
  • Need to preserve history

The actual situation: I have a git project that is named “drlaunch” and another git project that is named “debian”. “debian” is the packaging directory for drlaunch. The problem occurred because I used to have two svn trees, one for the project and one for the debian/ directory for making this a package for maemo.

I found a number of related things but all of them were complicated because they were doing more than I wanted. Finally, I came to this simple solution:

Under project drlaunch, there is a subdir drlaunch. I want to include the project debian under a directory named debian in the drlaunch project. The tree looks like this:

drlaunch (project)
-- drlaunch (dir)

debian (project)

And at the end I want it to look like this:

drlaunch (project)
|-- drlaunch (dir)
-- debian (dir)

The solution is as simple as this:

  1. Go to the debian’s project dir and export all changes with format-patch:[shell]
    cd debian/
    mkdir ../1
    git format-patch –root -o ../1/
    rm ../1/0000-*

    (Note: The removal of the first file is required. The file should be empty and for me it triggers a git bug causing it to use 100% cpu indefinitely. Feel free to check it yourself)

  2. Go to the drlaunch’s project dir and import all changes to a directory:
    cd drlaunch/
    git am –directory=debian/ ../1/*
  3. Ta-da! Ready! Now compare the tree to be sure that nothing bad happend:
    diff -uR debian ../debian/

Don’t forget to commit your changes.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.