0

It turns out a git repo I created betters suits as subdirectory, say module x, of a more general repository. How can I copy the original history (no signed commits) over such that now all the paths have the prefix module x\?

I could of course simply make moving everything into that subdirectory a new commit, but I'd prefer a solution that makes history look as if the former repo was always in the subdirectory.


I tried git subtree add --prefix="module x" ../module_x master from a temporary parallel folder with a fresh repository (since otherwise git subtree complains about the already existing prefix), but end up with

fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Working tree has modifications.  Cannot add.

This seems to be due to the lack of an initial commit, because if I

touch .gitignore
git add .gitignore
git commit -m"Init"

it works, but I end up with the original history in a separate branch plus a commit of moving, so just the manual solution I mentioned before...

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221

1 Answers1

0
git filter-branch --tree-filter 'mkdir -p module_x; mv all the files and* module_x/' HEAD

Did the trick. I'm using msysgit, but I guess in Linux shopt -s extglob; mv !(module_x) module_x/ should work even better...

If the files explicitly mentioned have not existed through the entire history, you may have to trick a bit and append ; true to the --tree-filter such that mv's non-zero exit code is ignored.

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221