3

I am working on a large code base that is not currently under any revision control (yes, really). I have been working on one component in a deep directory and created a covert git repository to track my changes. I now want to add all the code to source control and make a repository for the whole directory tree. I want the new, outer repository to include all the history for the files in the inner repository. I do not want separate projects or submodules; I want to be as if the repository had been created in the root of the code tree from the start and the inner repository never existed. I want to delete the .git directory in the sub-directory but would prefer not to lose the revision history that it contains.

Everything I have read on the topic is about merging existing repositories and that is not the case here or about maintaining submodules or remote projects, which I don't want.

Thank you.

Anthony
  • 31
  • 1

2 Answers2

1

Let's say your component is in /project-root/deep/path/to/component. The following commands will do what you want:

cd /project-root/deep/path/to/component
git filter-branch --tree-filter 'mkdir -p deep/path/to/component; git mv file1 file2 file2 deep/path/to/component' HEAD
rm -rf deep
mv .git /project-root
git reset --hard

Then of course you have to add the files for the rest of the project just as normal.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
0

You could use filter-branch and move existing files in the current history into the subdirectory where they would be had the repository existed at project root from the beginning on:

git filter-branch \
--tree-filter '
>/dev/null mkdir -p path/from/project/root/to/current/;
mv * path/from/project/root/to/current/;
# take care of special files, e.g. .gitignore and other hidden files
mv .gitignore path/from/project/root/to/current/;
'
--tag-filter cat \
--all

After this is finished, verify the resulting history. If everything is dandy, move the .git directory into your project root; you should not see any unstaged files.

I'm pretty sure there is a computationally cheaper way, using --index-filter and git's plumbing commands, but I don't know them by heart.

This will rewrite history, so don't use this method if your repository was shared between several developers.

knittl
  • 246,190
  • 53
  • 318
  • 364