如何合併兩個Git倉庫

假設有兩個Git倉庫:

  1. https://github.com/jiangxincode/thesis.git
  2. https://[email protected]/jiangxincode/thesis.git

現在需要進行合併,保留雙方的歷史提交記錄,並將1的內容刪除,合併之後的內容推送到2中。

從Github上clone倉庫到github目錄:

$ git clone https://github.com/jiangxincode/thesis.git github
Cloning into 'github'...
remote: Enumerating objects: 29, done.
remote: Total 29 (delta 0), reused 0 (delta 0), pack-reused 29
Unpacking objects: 100% (29/29), done.

從Bitbucket上clone倉庫到bitbucket目錄:

$ git clone https://[email protected]/jiangxincode/thesis.git bitbucket
Cloning into 'bitbucket'...
remote: Counting objects: 153, done.
remote: Compressing objects: 100% (150/150), done.
remote: Total 153 (delta 63), reused 0 (delta 0)
Receiving objects: 100% (153/153), 26.68 MiB | 2.64 MiB/s, done.
Resolving deltas: 100% (63/63), done.

在合併前根據實際情況分別處理兩個目錄的內容,並提交上傳到遠程倉庫。

在倉庫bitbucket中添加遠程倉庫github,命名爲github

$ cd bitbucket/
$ git remote add github ../github/
$ git remote
github
origin

檢出歷史信息

$ git fetch github
warning: no common commits
remote: Enumerating objects: 32, done.
remote: Counting objects: 100% (32/32), done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 32 (delta 8), reused 0 (delta 0)
Unpacking objects: 100% (32/32), done.
From ../github
 * [new branch]      master     -> github/master

基於github的master分支創建並檢出新的分支,名字爲github_master

$ git checkout -b github_master github/master
Switched to a new branch 'github_master'
Branch 'github_master' set up to track remote branch 'master' from 'github'.

切到倉庫2的主線分支

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

合併基於github創建的分支github_master到倉庫bitbucket的master分支上

$ git merge github_master  --allow-unrelated-histories
Auto-merging .gitignore
CONFLICT (add/add): Merge conflict in .gitignore
Automatic merge failed; fix conflicts and then commit the result.

此處可以看出出現了衝突,需要先解決衝突

$ git status
On branch master
Your branch is up to date with 'origin/master'.

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:

        new file:   tex-source/LICENSE
        new file:   tex-source/Makefile
        new file:   tex-source/abstract.tex
        new file:   tex-source/dtx-style.sty
        new file:   tex-source/englishabstract.tex
        new file:   tex-source/gbt7714-2005.bst
        new file:   tex-source/get_texmf_dir.sh
        new file:   tex-source/jiangxin.bib
        new file:   tex-source/jiangxin.tex
        new file:   tex-source/njulogo.eps
        new file:   tex-source/njuname.eps
        new file:   tex-source/njuthesis.cfg
        new file:   tex-source/njuthesis.cls
        new file:   tex-source/njuthesis.dtx
        new file:   tex-source/njuthesis.ins
        new file:   tex-source/preface.tex

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both added:      .gitignore

$ git add .gitignore

$ git commit
[master 3d6cb22] Merge branch 'github_master'

最後push本地所有分支到bitbucket

$ git push origin master
Enumerating objects: 37, done.
Counting objects: 100% (37/37), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (32/32), done.
Writing objects: 100% (35/35), 320.42 KiB | 3.56 MiB/s, done.
Total 35 (delta 9), reused 0 (delta 0)
To https://bitbucket.org/jiangxincode/thesis.git
   f92ef9e..3d6cb22  master -> master

$ git push origin github_master:github_master
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create pull request for github_master:
remote:   https://bitbucket.org/jiangxincode/thesis/pull-requests/new?source=github_master&t=1
remote:
To https://bitbucket.org/jiangxincode/thesis.git
* [new branch]      github_master -> github_master

此時查看本地和遠程的所有分支

$ git branch -a
  github_master
* master
  remotes/github/master
  remotes/origin/HEAD -> origin/master
  remotes/origin/github_master
  remotes/origin/master
  origin/master
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章