Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the opposite of `git filter-branch --subdirectory-filter`?

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.

like image 382
Septagram Avatar asked Nov 12 '12 03:11

Septagram


2 Answers

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.

like image 132
Jamey Sharp Avatar answered Sep 28 '22 02:09

Jamey Sharp


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
like image 23
qqx Avatar answered Sep 28 '22 01:09

qqx