With git filter-branch --subdirectory-filter
you can transform a subdirectory from the current repository into a repository on its own. What I'd like to do, however, is an opposite thing: create a repository that will contain the full contents of the current repository as a subdirectory, keeping the complete history as if they were always in that subdirectory. I don't want to use submodules, because I'd like that directory to be a completely integral part of the repository.
Background: I was tasked with writing a simple script, but the scope of task has been expanded, so now that script becomes just a subdirectory in a bigger project. It also has no point of living on it's own, so a solution without submodules is preferred.
The simplest solution is to leave the history alone: Just git mv
all your files into a subdirectory, commit that change, and carry on with your life.
If you really want to pretend as if you did this work in a subdirectory all along, the least complicated way is to use something like:
git filter-branch --tree-filter "mkdir SUBDIR && bash -c 'git mv -k {,.[!.],..[!.]}* SUBDIR/'"
Replace SUBDIR with the name of the target subdirectory.
You could just move the files within the subproject repository into the directory that you would like to have them in within the larger repository, then merge those commits into the final repository. This would fully preserve history of both repositories, including all of the commit IDs.
This could be done with commands similar to:
cd /path/to/subproject
mkdir subproject_dir
git mv file1 dir2 dir3... subproject_dir
git commit
cd /path/to/main_project
git pull /path/to/subproject
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With