سؤال

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

Use the git subtree command. Assuming that you have your new repo:

$ cd $ROOT_OF_NEW_REPO
$ git subtree add --prefix=subdirectory/for/old/repo <old git repo URL or checkout> <commit ref>

eg.

$ git subtree add --prefix=history ~/repos/oldrepo HEAD

The history directory will contain oldrepo files at the HEAD commit, along with all the history.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top