I have a directory structure in my repository:
|-repository/
| | |-repository/fileOne
| |
| |-subOne/
| | |-subOne/fileTwo
| |
| |-subTwo/
| |-subTwo/fileThree
I want to move the directory subOne
into a different repository. To do that, I'm separating subOne
using git-filter-branch
like so:
$ git filter-branch --subdirectory-filter subOne --prune-empty
This leaves me with a branch history that looks like this:
$ git log --oneline
0c0ea11 subOne history
6318bba subOne history
c96fddb Initial commit
This is exactly what I want; from another repository I can do a git fetch
and git merge
, which adds subOne
and history into the other repository. That's great.
However, git-filter-branch
has removed subOne
. Checking the directory structure on my branch:
$ ls
fileOne
$ pwd
/this/is/a/path/repository
pwd
should read: /this/is/a/path/repository
, but ls
should show subOne
inside the repository. What I've got instead is the files from subOne
moved upwards, and the directory removed.
How can I extract subOne
without losing the directory?
It turns out this is the default operation of
git-filter-branch --subdirectory-filter
. Quoting fromman git-filter-branch
:To fix this, I want to shift everything in
/
to/subdirectory
. Luckily, there's a piece of voodoo-looking code inman git-filter-branch
, underEXAMPLES
:EDIT: THIS WILL NOT WORK ON MAC OS X. See my question here for more information.
BSD sed
is not the same asGNU sed
, which is annoying. To run that example on OS X, changesed
togsed
; the filter then works quite well.