git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Merging two projects

From
Jeff King <peff@peff.net>
Date
Nov 4, 2008, 04:56 UTC
Message-ID
<20081104045644.GC31276@coredump.intra.peff.net>
In-Reply-To
<216e54900811031933n346e8c68se9226e79366c3eb6@mail.gmail.com>
On Mon, Nov 03, 2008 at 07:33:21PM -0800, Andrew Arnott wrote:
Show 6 quoted lines
> I have two projects, both with histories, that I want to merge into
> just one library.  I'd like to preserve both histories, perhaps as if
> there were two branches being merged into one.  (Although each project
> has its own branches that will make the history interesting).
> 
> Is there a standard way to do this?
You have two options:
 - just merge the history as if they were two branches:
     # make a new combined repo
     mkdir merged && cd merged && git init
     # pull in each library
     for lib in lib1 lib2; do
       git fetch /path/to/$lib master:$lib
       git checkout $lib
       # do any pre-merging fixups
       mkdir $lib
       mv * $lib
       git add -u
       git add $lib
       git commit -m "move files into $lib in preparation for project merge"
     done
     # and then merge; order isn't really important
     git checkout lib1
     git merge lib2
   This method is nice because it preserves the original histories, and
   you can still merge from the original projects (if people are still
   working on them as individual projects).
   Note that this is basically what the subtree merge strategy does, so
   you could also use that. I showed all the steps here to give a sense
   of what is going on, and because you might want to do additional
   fixups besides moving files into the subtree.
 - The other alternative is rewriting the history. The advantage here
   is that the history looks as if the projects had always been part of
   the repo, so there is no big rename event. You can even annotate the
   commit messages to indicate which project is being worked on.
   The downside, of course, is that having rewritten history, merges
   with the original project become more difficult. But that is not a
   problem if you are going to throw away the original repositories.
   You can accomplish this with filter-branch:
     mkdir merged && cd merged && git init
     for lib in lib1 lib2; do
       git fetch /path/to/$lib master:$lib
       git checkout $lib
       git filter-branch -f \
         --index-filter '
             git ls-files -s |
             sed "s,\t,&'$lib'/," |
             GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
               git update-index --index-info &&
               mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' \
         --msg-filter "sed '1s/^/[$lib] /'" \
         $lib
     done
   and then merge the resulting branches as usual.
Hope that helps.
-Peff
Previous: Andrew Arnott
Message 2 of 2 in “Merging two projects”
  1. Andrew ArnottNov 4, 2008
  2. Jeff KingNov 4, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.