Gangmax Blog

自由之思想,独立之精神

Combine Multiple Git Repositories Into One

| Comments

The purpose is to combine multiple Git repositories into one without losing the history information. From here(MiniQuark’s answer).

Imagine we have the following projects each one is a Git repository:

1
2
3
4
5
6
7
8
drwxrwxr-x 7 gang gang 4.0K 11月 28 15:13 AGS_APIMgr
drwxrwxr-x 5 gang gang 4.0K 11月 28 15:13 AGS_Gateway
drwxrwxr-x 5 gang gang 4.0K 11月 28 15:13 AGS_IPAAS_Admin
drwxrwxr-x 5 gang gang 4.0K 11月 28 15:14 AGS_ISPDev
drwxrwxr-x 2 gang gang 4.0K 11月 28 15:06 AGS_ISPOwner
drwxrwxr-x 2 gang gang 4.0K 11月 28 15:06 AGS_ISVDoc
drwxrwxr-x 4 gang gang 4.0K 11月 28 15:19 AGS_model
drwxrwxr-x 6 gang gang 4.0K 11月 28 15:14 AGS_OAuth

First, run the following commands inside each sub project directory(remember to change the project directory name in each running):

1
2
3
4
5
6
$ cd /root/AGS_APIMgr
$ git filter-branch --index-filter \
    'git ls-files -s | sed "s-\t-&AGS_APIMgr/-" |
     GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
     git update-index --index-info &&
     mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD

Second, run the following commands to create new Git repository, add content and remove original content:

1
2
3
4
5
6
cd root
git init
# Run the following commands for each sub project directory you're working on:
git pull AGS_APIMgr
rm -rf AGS_APIMgr/AGS_APIMgr
rm -rf AGS_APIMgr/.git

Done.

Comments