12

I have the following folder structure:

foo/
foo/bar
foo/baz
foo/bee

I created a git repository on foo/bar/.git.

I realized later on that I needed to include all the other directories in foo in a git repository, so I created a git repository on foo/.git.

foo/.git
foo/bar/.git
foo/baz
foo/bee

I could just delete foo/bar/.git but I want to be able to keep the history of that git repository but within foo/.git instead of foo/bar/.git.

I was trying to read on submodules but if I understand correctly it is a way to integrate a version of an existing repository as a subtree of your own repository.

I'm not entirely sure this is what I need. I don't need to keep foo/bar/.git. I want the foo/.git to be my main repository. I just wanted to know if there was a way to keep or integrate the history of the foo/bar/.git within foo/.git so I could remove foo/bar/.git.

stormwild
  • 2,855
  • 2
  • 31
  • 38

3 Answers3

9

Why not create a bar directory in foo/bar, git mv everything into foo/bar/bar, then move (without involving git) foo/bar/.git to .git and the contents of foo/bar/bar up to foo/bar finally removing the now empty foo/bar/bar subfolder.

Then you can add the rest of the subdirectories to the git repository which is now in foo.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • But I already have foo/.git. Do you mean I should replace foo/.git with foo/bar/.git? – stormwild May 03 '12 at 11:01
  • 1
    @stormwild: I'm sorry, I misunderstood. I thought that `foo/.git` was the new repository that you wanted to create. It sounds like you'll have to do some sort of baseless merge if you already have two .git repositories that you want to merge. – CB Bailey May 03 '12 at 11:08
  • I think your solution still applies since I just newly created the parent repository. Thanks :) – stormwild May 03 '12 at 11:22
  • I think the rebase thing is provided by the solution here: http://stackoverflow.com/questions/614229/can-i-move-the-git-directory-for-a-repo-to-its-parent-directory/614254 which is similar to sehe's answer. – stormwild May 03 '12 at 11:26
6

You are looking for subtree merging

Github's help has a good guide on this, and there's a chapter in Pro Git on this, same with the Git Community book

dbr
  • 165,801
  • 69
  • 278
  • 343
3

I suggest using a single invocation of filter-branch on a clone of your repository:

git filter-branch --index-filter 'git ls-files -s | sed "s-\t\"*-&bar/-" |
           GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info &&
         mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE'  --tag-name-filter cat -- --all

This will move all files into a subdirectory 'bar' rewriting all revision history (all branches and tags).

Now you can treat the rewritten history as the new repository.

(Command adapted from a sample in man git-filter-branch):


Optional background info

To remove old cruft/reflogs/filter branch backups, do

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --expire=now --all
git gc --prune=now

Of course you can also just clone the repo to get rid of the backup/garbage.

sehe
  • 374,641
  • 47
  • 450
  • 633