tags:

views:

838

answers:

4

I have a (Windows) GIT repository in a folder called XXX, and I have second GIT repository called YYY.

I want to import the XXX repository into the YYY repository, add all XXX's change history to YYY, and rename XXX to ZZZ.

Folder structure before:

XXX
 |- .git
 |-  ZZZ

Folder structure after:

YYY
 |- .git  <-- This now contains the change history from XXX
 |-  ZZZ  <-- This was originally XXX
 |-  (other folders)

Can this be done, or must I resort to using sub-modules?

A: 

I think you can do this using 'git mv' and 'git pull'.

I'm a fair git noob - so be careful with your main repository - but I just tried this in a temp dir and it seems to work.

First - rename the structure of XXX to match how you want it to look when it's within YYY:

cd XXX
mkdir tmp
git mv ZZZ tmp/ZZZ
git mv tmp ZZZ

Now XXX looks like this:

XXX
 |- ZZZ
     |- ZZZ

Now use 'git pull' to fetch the changes across:

cd ../YYY
git pull ../XXX

Now YYY looks like this:

YYY
 |- ZZZ
     |- ZZZ
 |- (other folders that already were in YYY)
Aaron
A: 

I don't know of an easy way to do that. You COULD do this:

  1. Use git filter-branch to add a ZZZ super-directory on the XXX repository
  2. Push the new branch to the YYY repository
  3. Merge the pushed branch with YYY's trunk.

I can edit with details if that sounds appealing.

Walter Mundt
+4  A: 

There is a well-known instance of this in the Git repository itself, which is collectively known in the Git community as "the coolest merge ever" (after the subject line Linus Torvalds used in the e-mail to the Git mailinglist which describes this merge). In this case, the gitk Git GUI which now is part of Git proper, actually used to be a seperate project. Linus managed to merge that repository into the Git repository in a way that

  • it appears in the Git repository as if it had always been developed as part of Git,
  • all the history is kept intact and
  • it can still be developed independently in its old repository, with changes simply being git pulled.

The e-mail contains the steps needed to reproduce, but it is not for the faint of heart: first, Linus wrote Git, so he probably knows a bit more about it than you or me, and second, this was almost 5 years ago and Git has improved considerably since then, so maybe it is now much easier.

In particular, I guess nowadays one would use a gitk submodule, in that specific case.

Jörg W Mittag
BTW. the strategy used for subsequent merges (if there are any) is called **subtree** merge, and there is third party `git-subtree` tool which can help you with this: http://github.com/apenwarr/git-subtree
Jakub Narębski
Thanks, I forgot about that. The `subtree` merge strategy, especially in conjunction with the `git-subtree` tool is a nice, maybe even superior alternative to submodules.
Jörg W Mittag
+3  A: 

Probably the simplest way would be to pull the XXX stuff into a branch in YYY and the merge it into master:

In YYY:

git remote add other /path/to/XXX
git fetch other
git checkout -b ZZZ other/master
mkdir ZZZ
git mv stuff ZZZ/stuff             # as necessary
git checkout master                # should add ZZZ to master
git commit
git remote rm other
git push                           # if you have a remote, that is

I actually just tried this with a couple of my repos and it works. Unlike Jörg's answer it won't let you continue to use the other repo, but I don't think you specified that anyway.

ebneter
Thanks. I used a slightly modified version of your technique: I created a 'staging' branch on XXX where I created the ZZZ folder, and moved the 'stuff' into it. Then I merged XXX into YYY.
Vijay Patel