5

I would like to to a pretty standard repository split-up. The original repository has to following structure:

root
|-AAA
|-BBB
  |-BBB1
  |-BBB2
|-CCC

I tried to extract the directory BBB1 and all its contents into an individual repository using the command

git filter-branch --subdirectory-filter BBB/BBB1 -- --all

which leads to git rewriting a bunch of commits and ends with git's final statement

Ref 'refs/heads/master' was rewritten

So this seems perfectly fine for me. But when I take a look into the root directory after git has finished, the repository no longer contains any source files but only the .git folder at root. Browsing this repository with SourceTree I can see in the log the (probably) correct commits which would have affected BBB1 only, but each commit no longer references any files as they are all gone!

What am I doing wrong? Why are all the files gone, even the ones I actually filtered for?

Thanks in advance!

Simon

Alex T.
  • 174
  • 1
  • 5
Franz Xaver
  • 81
  • 1
  • 3

2 Answers2

11

Make sure you are using Unix-style path separators (/) even on Windows machines.

This results in an empty repo with the correctly-filtered history (\):

git filter-branch --subdirectory-filter BBB\BBB1 -- --all

This results in your sub-directory pulled up to the root of the repo and the correctly-filtered history (/):

git filter-branch --subdirectory-filter BBB/BBB1 -- --all

Since you tend to lose the ability to auto-complete when using / directory separation in the Windows command line, it may be easier to create your path and then alter it to Unix-style separation.

Disclaimer

I know the original question shows / separators, but if you pass \ separators in the sub-directory argument, you see the exact same end result described, which has me skeptical. As soon as you flip to Unix-style / separators on a Windows machine, it works perfectly.

patridge
  • 26,385
  • 18
  • 89
  • 135
  • 1
    This also applies to `git-filter-repo`... Spent too much time trying to figure this out. Used unix-style path separators instead of Windows separators and it worked after that. – Tur1ng Jan 27 '23 at 20:47
1

I have experienced exactly the same problem and was not able to resolve it using filter-branch command nor did I found any explanations for the problem. But there is another way to achieve similar result that has worked for me. It's based on a subtree command. I found a reference to that approach here Detach (move) subdirectory into separate Git repository and an example of using it here https://makingsoftware.wordpress.com/2013/02/16/using-git-subtrees-for-repository-separation/. I followed that article and it worked totally fine for me.

Community
  • 1
  • 1
Alex T.
  • 174
  • 1
  • 5