7

I have a repository within a subdirectory, and now I would like to have the parent directories under version control too, but keep the current history of the repository.

So with the following tree:

a/b/c

c is currently holding a repository, but I would like to have a repository in a, and include all the history within c.

I'm sure there is a way to go around doing this, perhaps with some git filter-branch? I do not want to use submodules, I would like to have a single repository in the end.

3 Answers 3

3

What you need is to create your new outer repository, and merge the inner one using a subtree merge. There's a quick HOWTO at the bottom of this page.. You should temporarily move c outside of a/b, these instructions expect the c tree doesn't exist at its final location yet.


If you want to make c appear nested in its past history (and are okay with changed sha1s), use filter-branch instead (credit):

git filter-branch --index-filter \
    'git ls-files -s | sed "s#\t#&a/b/c/#" |
            GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
                    git update-index --index-info &&
     mv -T "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' -- --all
4
  • This solution will preserve the SHA1's of your existing commits, but all existing commits will still have the tree structure you have today. Starting at the merge point, your tree structure will reflect the deeper repo that you want. If you're happy to have a shallow tree structure in history, then this is probably the way you want to go about it. Commented Jan 3, 2012 at 21:45
  • This works almost perfectly, except for the comment of @Kevin. Indeed, that is what happens with the history. It's not a huge deal, but how would I go about changing the path of the merged commits? For completion, this is what I used: git remote add c_new -f ../c_repo; git read-tree --prefix=b/c/ -u c_new/master; git commit -m 'Merge b/c into a'; git pull -s subtree c_new master; git remote rm c_new
    – englebip
    Commented Jan 3, 2012 at 22:13
  • sha1s will change. Filter branch is not something that I would recommend on a repo that is in the middle of a project. I reserve this for rare cases where I'm starting a new project off of an old one. Commented Jan 3, 2012 at 22:52
  • your first link is broken. I believe this this the page you're trying to link to: Git-Tools-Advanced-Merging Commented Dec 30, 2023 at 2:01
2

You have a choice:

You can simply move the current structure into what will be the new structure. Then move the whole repo a level up. git add -A will add all the changes which is a bunch of deletions and new file adds. git status will show them as moved. Each object is stored once, no matter what path it's on so this won't be an expensive operation. This would be the simplest option. At this time you can also put in the other high level directories.

while in c:

mkdir b
cd b
mkdir c
mv <all other objects from top level> c
git add -A
git commit -m "moved everything"
mv c a
# clean up

git will determine the fact that a file was moved if you are doing merges and help you.

Or, you can use filter branch to change history if you like, but all your commits will have different SHA1s. This may be undesireable.

2
  • This is a simple, clever solution to the problem. It still leaves with the issue that the historical paths are not accurate (which can be changed with the filter-branch command provided by @Tobu), but the history is linear and intuitive as to the evolution of the repo. Thanks!
    – englebip
    Commented Jan 3, 2012 at 22:53
  • A couple of remarks regarding the commands above: 1) the first three could me merged into mkdir -p b/c, and then changing the destination of the mv <all other objects from top level> b/c; 2) do not be (foolishly) tempted as I was to do for this last command mv * b/c; you will recursively copy b/c/b/c/b/c... until filling up your hard drive.
    – englebip
    Commented Jan 3, 2012 at 22:56
0

Wouldn't a simple mv work? All of 'c' would just show up as a rename.

Something like this: (please don't cut and paste exactly as I haven't tried this.. just get the gist..)

cp -r /someroot/a/b/c /tmp/tmproot/c    # make a full backup of the given repository
rm -Rf /someroot/a/b/c                  # completely remove c, leaving only a/b
mv -r /someroot/a /tmp/tmproot        # moves a/b to the repository
cd /tmp/tmproot
git mv -r c a/b                         # git-moves c into a/b, so all history of c is retained

now your /tmp/tmproot directory should have a/b/c with the history of c retained

rm /someroot
mv -Rf /tmp/tmproot /someroot               # replace the old rep with your new one

now add everything and commit

1
  • This is indeed a good idea, and very similar to @Adam's. I tried that one, and it worked well. Thanks!
    – englebip
    Commented Jan 3, 2012 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.