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 https://launchpad.net/~git-core/+archive/candidate .
 More info: https://launchpad.net/~git-core/+archive/ubuntu/ppa
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 keyserver.ubuntu.com
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)
OK
$ sudo apt-get update 
Ign:1 http://dl.google.com/linux/chrome/deb stable InRelease
Hit:2 http://dl.google.com/linux/chrome/deb stable Release                     
Hit:4 https://apt.dockerproject.org/repo ubuntu-xenial InRelease               
Ign:5 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 InRelease      
Ign:6 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 InRelease      
Hit:7 http://us.archive.ubuntu.com/ubuntu xenial InRelease          
Get:8 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]     
Get:9 http://ppa.launchpad.net/git-core/ppa/ubuntu xenial InRelease [17.5 kB]  
Hit:10 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 Release       
Get:11 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]   
Hit:13 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release       
Hit:14 http://ppa.launchpad.net/ondrej/php/ubuntu xenial InRelease             
Hit:16 https://packages.gitlab.com/gitlab/gitlab-ee/ubuntu xenial InRelease    
Hit:17 http://ppa.launchpad.net/ondrej/php-zts/ubuntu xenial InRelease         
Get:18 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB] 
Hit:19 http://ppa.launchpad.net/phalcon/stable/ubuntu xenial InRelease         
Hit:20 http://ppa.launchpad.net/webupd8team/sublime-text-3/ubuntu xenial InRelease
Get:21 http://ppa.launchpad.net/git-core/ppa/ubuntu xenial/main amd64 Packages [3,256 B]
Get:22 http://ppa.launchpad.net/git-core/ppa/ubuntu xenial/main i386 Packages [3,248 B]
Get:23 http://ppa.launchpad.net/git-core/ppa/ubuntu 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:
  git-man
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 http://ppa.launchpad.net/git-core/ppa/ubuntu xenial/main amd64 git amd64 1:2.13.0-0ppa1~ubuntu16.04.1 [4,726 kB]
Get:2 http://ppa.launchpad.net/git-core/ppa/ubuntu 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

Leave a Reply

Your email address will not be published. Required fields are marked *