将许多子目录拆分为一个新的独立Git存储库

Jas*_*son 5 git git-filter-branch git-subtree

这个问题与以下内容有关:

我想分离一对,而不是分离一个子目录.例如,这是我的文件夹结构:

/app1
  /file1
  /file2
  /folder1
  /folder2
/app2
  /file3
  /file4
  /folder3
  /folder4
/test
  /file5
  /file6
  /folder5
  /folder6
/README.md
/new_file
Run Code Online (Sandbox Code Playgroud)

而我想这样做:

/app1
  /file1
  /file2
  /folder1
  /folder2
/app2
  /file3
  /file4
  /folder3
  /folder4
Run Code Online (Sandbox Code Playgroud)

我尝试了以下替代方案,到目前为止还没有工作:

git filter-branch --index-filter 'git rm --cached -qr -- . && git reset -q $GIT_COMMIT -- /app1/* app2/*' --prune-empty -- --all

Error: 
    pathspec '.' did not match any files
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

git filter-branch --tree-filter "mkdir samples; mv app1 app2 samples/" HEAD

Error:
   rename app1 to samples/app1: No such file or directory
   Same error for the second sub-directory
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

jor*_*ran 6

在树形过滤器的情况下,您将评估每个提交的脚本,甚至可能不存在app1和app2的提交,这可以通过一个保护和一个不会失败的替代命令来解决.

 git filter-branch --tree-filter "mkdir samples && test -d app1 && mv app1 samples/|| test -d app2 && mv app2 samples/ || echo 'nothing to do'" HEAD
Run Code Online (Sandbox Code Playgroud)

然后将样本作为根目录,这将把app1和app2移动到项目的根目录.

git filter-branch -f --subdirectory-filter samples -- --all
Run Code Online (Sandbox Code Playgroud)

我建议你先做备份!