Git merge in a smaller repository

I’m using local-only (ie no remotes) Git repositories for quite a few directories – mostly my notes, but some code, and some LaTeX documents. I make heavy use of .gitignore to screen out non-texty files. I decided to make my IT notes directory into a super-repository, and I wanted it to seamlessly pick up the history of a sub-directory that had been a Git repository on its own.

  • D:\Dropbox\IT – location for my new super-repository, just for my use (no remotes), with loads of subdirectories
  • D:\Dropbox\egreg – a flat directory, containing just a few files and an active Git repository
  • D:\Dropbox\IT\Cross-platform\LaTeX\ – the parent directory that egreg used to live under (but was temporarily moved out of while I made IT into a super-repository)


First, I prepare the smaller repository, D:\Dropbox\egreg, for the change of location (- see below); then, in the root of the big repository, D:\Dropbox\IT, I just pull in the smaller with: git pull /d/dropbox/egreg. To tidy up, I delete the no-longer needed D:\Dropbox\egreg.

prepare a repository to be integrated into another

All we do is move the files to an appropriate subfolder, in this case, to Cross-platform\LaTeX\egreg, and have them seem to’ve always been there. This involves a few steps (one of which is truly weird):

1. empty the repository’s root folder: rm -r *

2. reconstitute only the Git-indexed files: git checkout-index -f -a

3. move the whole tree into a subdirectory (the weird step):

git filter-branch --index-filter \
  'git ls-files -s | sed "s#\t\"*#&Cross-platform\/LaTeX\/egreg/#" |
  GIT_INDEX_FILE=$ git update-index --index-info &&
  mv "$" "$GIT_INDEX_FILE"' HEAD

4. check the results with ls -a Cross-platform/LaTeX/egreg

5. remove .gitignore (and .gitattributes, if it’s there):

rm Cross-platform/LaTeX/egreg/.gitignore

6. stage and commmit the deletion:

git add . A
git commit -m ".gitignore removed"

7. check the (now adjusted) history with gitk.

– the repository should now ready to be git pull‘d from another repository, it’s history preserved.

Side note: yes, I’ve a Git repository in my Dropbox, no conflicts yet.

You can read more of my Git notes in my IT DokuWiki.


Share Button