Question:
I started a project some months ago and stored everything within a main directory.
In my main directory “Project” there are several subdirectories containing different things:
Project/paper contains a document written in LaTeX
Project/sourcecode/RailsApp contains my rails app.
“Project” is GITified and there have been a lot of commits in both “paper” and “RailsApp” directory. Now, as I’d like to use cruisecontrol.rb for my “RailsApp” I wonder if there is a way to make a submodule out of “RailsApp” without losing the history.
Answer:
Nowadays there’s a much easier way to do it than manually using git filter-branch: git subtree
Installation
NOTE git-subtree
is now part of git
(if you install contrib) as of 1.7.11, so you might already have it installed. You may check by executing git subtree
.
To install git-subtree from source (for older versions of git):
1 2 3 4 5 |
git clone https://github.com/apenwarr/git-subtree.git cd git-subtree sudo rsync -a ./git-subtree.sh /usr/local/bin/git-subtree |
Or if you want the man pages and all
1 2 3 |
make doc make install |
Usage
Split a larger into smaller chunks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Go into the project root cd ~/my-project # Create a branch which only contains commits for the children of 'foo' git subtree split --prefix=foo --branch=foo-only # Remove 'foo' from the project git rm -rf ./foo # Create a git repo for 'foo' (assuming we already created it on github) mkdir foo pushd foo git init git remote add origin git@github.com:my-user/new-project.git git pull ../ foo-only git push origin -u master popd # Add 'foo' as a git submodule to `my-project` git submodule add git@github.com:my-user/new-project.git foo |
For detailed documentation (man page), please read git-subtree.txt
.