将许多子目录分离成一个新的单独的Git存储库 [英] Detach many subdirectories into a new, separate Git repository
问题描述
这个问题基于将子目录分离到单独的Git存储库 p>
不是分离一个子目录,而是分离一对。例如,我目前的目录树如下所示:
/ apps
/ AAA
/ BBB
/ CCC
/ libs
/ XXX
/ YYY
/ ZZZ
我想代之以:
/ apps
/ AAA
/ libs
/ XXX
- 子目录过滤器参数到
git filter-branch
将不起作用,因为它在第一次运行时除去给定目录以外的所有内容。我认为对所有不需要的文件使用 - index-filter
参数可能会有效(尽管很乏味),但如果我尝试多次运行它,我会收到以下消息:
无法创建新的备份。
以前的备份已经存在于refs / original /
中强制使用-f
$ b $覆盖备份b
有什么想法? TIA
在这里回答我自己的问题......经过大量的试验和错误。
我设法使用 git subtree
和 git-stitch-repo
。这些指令基于:
- 使用git subtree在项目之间共享代码 - http://psionides.jogger.pl/2010/02/04/sharing-code-between- projects-with-git-subtree /
- 作业的正确工具:git-stitch-repo - http://ifup.org/2009/02/07/the-right-tool- for-the-job-git-stitch-repo /
首先,我拿出了我想保留的目录进入他们自己的单独存储库:
cd origRepo
git subtree split -P apps / AAA -b aaa
git subtree split -P libs / XXX -b xxx
cd ..
mkdir aaaRepo
cd aaaRepo
git init
git fetch ../ origRepo aaa
git checkout -b master FETCH_HEAD
cd ..
mkdir xxxRepo
cd xxxRepo
git init
git fetch ../origRepo xxx
git checkout -b master FETCH_HEAD
然后我创建了一个新的空存储库,并导入/缝合最后两个:
cd ..
mkdir newRepo
cd newRepo
git init
git-stitch-repo ../aaaRepo:apps/AAA ../xxxRepo:libs/XXX | git fast-import
这会创建两个分支, master-A
和 master-B
,每个包含一个拼接仓库的内容。将它们合并并清理:
git checkout master-A
git pull。 master-B
git checkout master
git branch -d master -A
git branch -d master -b
现在我不太确定如何/何时发生这种情况,但在第一个结帐
和拉
,代码奇迹般地合并到主分支中(任何有关这里发生的事情的信息都会被赞赏!)
一切似乎都起作用除了如果我查看 newRepo
提交历史记录,当变更集同时影响 apps / AAA
和 libs / XXX
。如果有一种方法可以删除重复项,那么它将是完美的。
This question is based on Detach subdirectory into separate Git repository
Instead of detaching a single subdirectory, I want to detach a couple. For example, my current directory tree looks like this:
/apps
/AAA
/BBB
/CCC
/libs
/XXX
/YYY
/ZZZ
And I would like this instead:
/apps
/AAA
/libs
/XXX
The --subdirectory-filter
argument to git filter-branch
won't work because it gets rid of everything except for the given directory the first time it's run. I thought using the --index-filter
argument for all unwanted files would work (albeit tedious), but if I try running it more than once, I get the following message:
Cannot create a new backup.
A previous backup already exists in refs/original/
Force overwriting the backup with -f
Any ideas? TIA
Answering my own question here... after a lot of trial and error.
I managed to do this using a combination of git subtree
and git-stitch-repo
. These instructions are based on:
- Sharing code between projects with git subtree – http://psionides.jogger.pl/2010/02/04/sharing-code-between-projects-with-git-subtree/
- The Right Tool for the Job: git-stitch-repo – http://ifup.org/2009/02/07/the-right-tool-for-the-job-git-stitch-repo/
First, I pulled out the directories I wanted to keep into their own separate repository:
cd origRepo
git subtree split -P apps/AAA -b aaa
git subtree split -P libs/XXX -b xxx
cd ..
mkdir aaaRepo
cd aaaRepo
git init
git fetch ../origRepo aaa
git checkout -b master FETCH_HEAD
cd ..
mkdir xxxRepo
cd xxxRepo
git init
git fetch ../origRepo xxx
git checkout -b master FETCH_HEAD
I then created a new empty repository, and imported/stitched the last two into it:
cd ..
mkdir newRepo
cd newRepo
git init
git-stitch-repo ../aaaRepo:apps/AAA ../xxxRepo:libs/XXX | git fast-import
This creates two branches, master-A
and master-B
, each holding the content of one of the stitched repos. To combine them and clean up:
git checkout master-A
git pull . master-B
git checkout master
git branch -d master-A
git branch -d master-B
Now I'm not quite sure how/when this happens, but after the first checkout
and the pull
, the code magically merges into the master branch (any insight on what's going on here is appreciated!)
Everything seems to have worked as expected, except that if I look through the newRepo
commit history, there are duplicates when the changeset affected both apps/AAA
and libs/XXX
. If there is a way to remove duplicates, then it would be perfect.
这篇关于将许多子目录分离成一个新的单独的Git存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!