You want a git filter-branch that can move the entire repository into a subtree, keeping the history, making it look like it always has been. Back up your vault before using this!
Here is the magic. In /foo/bar run:
git filter-branch --commit-filter ' TREE="$1"; shift; SUBTREE=`echo -e 040000 tree $TREE"\tbar" | git mktree` git commit-tree $SUBTREE "$@"' -- --all
This will make the /foo/bar repository another subdirectory of "bar" with all its contents throughout its history. Then you can move the entire repo to the foo level and add the baz code to it.
Update:
Ok, here's what happens. The end is a link to the "tree" (think of it as a SHA representing the contents of a subdirectory of the entire file system) plus some "parent" SHAs and some authors / metadata links / messages, etc. The git commit-tree command is a low-level bit that brings it all together. The --commit-filter parameter is processed as a shell function and is run instead of git commit-tree during the filtering process and should act like it.
What I am doing is taking the first parameter, the original tree to commit, and creating a new βtree objectβ that talks about this in the subfolder using git mktree , another low-level git command. To do this, I need to enclose something similar to the git tree in it, that is, a set (type SP of type SP SHA TAB filename) of strings; thus the echo command. The output signal mktree then replaced with the first parameter when the I-chain is before the real commit-tree ; "$@" is a way to pass all other parameters without changes, dividing the first by shift . See git help mktree and git help commit-tree .
So, if you need several levels, you need to nest several additional levels of tree objects (this is not tested, but the general idea):
git filter-branch --commit-filter ' TREE="$1" shift SUBTREE1=`echo -e 040000 tree $TREE"\tbar" | git mktree` SUBTREE2=`echo -e 040000 tree $SUBTREE1"\tb" | git mktree` SUBTREE3=`echo -e 040000 tree $SUBTREE2"\ta" | git mktree` git commit-tree $SUBTREE3 "$@"' -- --all
This should move the real content down a/b/bar (note the reverse order).
Update . Integrated improvements. Matvey Alpert below. Without -- --all this only works in the current verified branch, but since the question asks a question about the general repo, it makes sense to do it like a fork.
Walter Mundt Jul 09 '10 at 12:50 2010-07-09 12:50
source share