Merge multiple git repositories

建議 git 版本 2.9 以上

升級 git

原因是 2.9 的版本以上,在 git merge 時可以下參數 ––allow-unrelated-histories

# 如果要升級 git 的話可以照以下步驟
$ sudo add-apt-repository ppa:git-core/ppa
 The most current stable version of Git for Ubuntu.

For release candidates, go to .
 More info:
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpwbyj0klz/secring.gpg' created
gpg: keyring `/tmp/tmpwbyj0klz/pubring.gpg' created
gpg: requesting key E1DF1F24 from hkp server
gpg: /tmp/tmpwbyj0klz/trustdb.gpg: trustdb created
gpg: key E1DF1F24: public key "Launchpad PPA for Ubuntu Git Maintainers" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
$ sudo apt-get update 
Ign:1 stable InRelease
Hit:2 stable Release                     
Hit:4 ubuntu-xenial InRelease               
Ign:5 xenial/mongodb-org/3.2 InRelease      
Ign:6 xenial/mongodb-org/3.4 InRelease      
Hit:7 xenial InRelease          
Get:8 xenial-security InRelease [102 kB]     
Get:9 xenial InRelease [17.5 kB]  
Hit:10 xenial/mongodb-org/3.2 Release       
Get:11 xenial-updates InRelease [102 kB]   
Hit:13 xenial/mongodb-org/3.4 Release       
Hit:14 xenial InRelease             
Hit:16 xenial InRelease    
Hit:17 xenial InRelease         
Get:18 xenial-backports InRelease [102 kB] 
Hit:19 xenial InRelease         
Hit:20 xenial InRelease
Get:21 xenial/main amd64 Packages [3,256 B]
Get:22 xenial/main i386 Packages [3,248 B]
Get:23 xenial/main Translation-en [2,496 B]
Fetched 333 kB in 2s (154 kB/s)                                
Reading package lists... Done
$ sudo apt-get install git
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
Suggested packages:
  git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk
  gitweb git-arch git-cvs git-mediawiki git-svn
The following packages will be upgraded:
  git git-man
2 upgraded, 0 newly installed, 0 to remove and 246 not upgraded.
Need to get 6,174 kB of archives.
After this operation, 6,388 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 xenial/main amd64 git amd64 1:2.13.0-0ppa1~ubuntu16.04.1 [4,726 kB]
Get:2 xenial/main amd64 git-man all 1:2.13.0-0ppa1~ubuntu16.04.1 [1,448 kB]
Fetched 6,174 kB in 7s (859 kB/s)                                              
(Reading database ... 343803 files and directories currently installed.)
Preparing to unpack .../git_1%3a2.13.0-0ppa1~ubuntu16.04.1_amd64.deb ...
Unpacking git (1:2.13.0-0ppa1~ubuntu16.04.1) over (1:2.7.4-0ubuntu1.1) ...
Preparing to unpack .../git-man_1%3a2.13.0-0ppa1~ubuntu16.04.1_all.deb ...
Unpacking git-man (1:2.13.0-0ppa1~ubuntu16.04.1) over (1:2.7.4-0ubuntu1.1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up git-man (1:2.13.0-0ppa1~ubuntu16.04.1) ...
Setting up git (1:2.13.0-0ppa1~ubuntu16.04.1) ...
$ git --version 
git version 2.13.0

Merge two git repositories

# clone 1st project
$ git clone git@REPOSITORY_URL:project1.git
$ cd project1

# add 2nd project into remote source
$ git remote add -f proj_2 git@REPOSITORY_URL:project2.git

# checkout the branch you want to keep, for example: origin/master
$ git checkout BRANCH_1
HEAD is now at 10eb427...

# then merge the other branch, for example: project2/master
$ git merge --allow-unrelated-histories BRANCH_2

# after resolving conflicts, commit and push back to remote branch, for example: origin/master
$ git push origin HEAD:master --force


你的電子郵件位址並不會被公開。 必要欄位標記為 *