假设我有一个这样的设置
phd/code/
phd/figures/
phd/thesis/
由于历史原因,这些都有自己的git存储库。但是我想把它们合并成一个,这样可以简化一些。例如,现在我可能要做两组更改,并且必须做一些类似的事情
cd phd/code
git commit
cd ../figures
git commit
(现在)只要能表演就好了
cd phd
git commit
似乎有几种方法可以使用子模块或从我的子存储库中提取,但这比我想要的要复杂一些。至少让我满意的
cd phd
git init
git add [[everything that's already in my other repositories]]
但这似乎不是一句俏皮话。git中有什么可以帮助我的吗?
借助IntelliJ IDEA Community Edition中的git集成,我手动将3个git存储库合并为一个。
Create a new repo, add a new commit to the master branch with an empty README.md file.
Add three remotes for the new repo, using the name of the 3 repositories and the remote URL of them respectively. Run Git Fetch.
Create a new local branch named temp based on the master branch, so we can start over without pollute the master branch. Checkout the temp branch.
Select to only show commits of one remote branch(one repository).
Select all the commits and right click to Cherry-Pick them.
Create directory structure for this repository, then move the files into it and commit.
Repeat the step 4 to 6 for the other 2 remote branch(repository).
When everything is OK, merge all the changes in the temp branch into master branch.
然后添加主分支的原始远程URL并推送到它。
git-filter-branch解决方案工作得很好,但请注意,如果你的git repo来自SVN导入,它可能会失败,并发出如下消息:
Rewrite 422a38a0e9d2c61098b98e6c56213ac83b7bacc2 (1/42)mv: cannot stat `/home/.../wikis/nodows/.git-rewrite/t/../index.new': No such file or directory
在这种情况下,你需要从过滤器分支中排除最初的修订——即将最后的HEAD更改为[SHA of second revision]。头部-见:
http://www.git.code-experiments.com/blog/2010/03/merging-git-repositories.html
也许,简单地(类似于前面的答案,但使用更简单的命令)在每个单独的旧存储库中进行提交,将内容移动到一个适当命名的子目录中,例如:
$ cd phd/code
$ mkdir code
# This won't work literally, because * would also match the new code/ subdir, but you understand what I mean:
$ git mv * code/
$ git commit -m "preparing the code directory for migration"
然后将三个单独的回购合并为一个新的,通过这样做SMTH:
$ cd ../..
$ mkdir phd.all
$ cd phd.all
$ git init
$ git pull ../phd/code
...
然后您将保存历史记录,但将继续进行单个回购。
合并mainProject中的secondProject:
A)在第二个项目中
git fast-export --all --date-order > /tmp/secondProjectExport
B)在主项目中:
git checkout -b secondProject
git fast-import --force < /tmp/secondProjectExport
在这个分支中完成你需要做的所有繁重的转换并提交它们。
C)然后回到主分支和两个分支之间的经典合并:
git checkout master
git merge secondProject