如何合并两个Git仓库

假设有两个Git仓库:

  1. github: https://github.com/jiangxincode/thesis.git
  2. bitbucket: https://jiangxincode@bitbucket.org/jiangxincode/thesis.git

现在需要进行合并,保留双方的历史提交记录,并将github的内容删除,合并之后的内容推送到bitbucket中。

从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://jiangxincode@bitbucket.org/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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 多种多样的工作流使得在项目中实施Git时变得难以选择。这份教程提供了一个出发点,调查企业团队最常见的Git工作流。...
    JSErik阅读 4,311评论 2 8
  • 一、基本概念: 注:对于git的分布式概念及其优点,不重复说明,自己百度或谷歌。本文中涉及到指令前面有$的,在cm...
    大厂offer阅读 1,335评论 0 3
  • 远程仓库 到目前为止,我们已经掌握了如何在Git仓库里对一个文件进行时光穿梭,你再也不用担心文件备份或者丢失的问题...
    归云丶阅读 1,885评论 0 5
  • Git 是目前最流行的分布式版本控制系统之一。 版本控制指的是,记录每次版本变更的内容和时间等细节,保留各版本之间...
    神齐阅读 1,322评论 0 7
  • 每日导图: 格式:1205(日期)–SWSZ605 你的姓名:崔爱萍 作业总结:最初先听了语音,之后阅读文章时,发...
    Amy_53cf阅读 212评论 2 3