git 本地A仓库中,合并B仓库中的代码
需求情形一:
项目原来在V1仓库中,现在又把代码同步到V2仓库中,V1还在继续开发,现在需要将V1仓库的最新代码,都同步到V2仓库中。
既: 本地仓库V2, 需要合并仓库V1
1. 在V2的master 分支上,切出来一个新的分支(命名gitlab-code-branch)
git checkout -b gitlab-code-branch
2. 在仓库V2新的分支(gitlab-code-branch)上,加仓库V1的引用,并起别名
git remote add 仓库V1别名 仓库V1地址
git remote add gitlabRepo http://baidu/cp014000-1/cp014020/v2.0.git
3. 在V2新分支(gitlab-code-branch) 接收仓库V1 代码
git pull 仓库V1别名 接收分支名
git pull gitlabRepo gitlab-code-branch
4. 将仓库V2新分支(gitlab-code-branch) 推到远端,本地切到V2的master 上,再把远端的 gitlab-code-branch 分支拉到 master ,最后推到远端master
1.加仓库B的引用,并起别名
git remote add 仓库B别名 仓库B地址
git remote add gitlabRepo git@gitlab.cloopm.git
2. 查看引用是否成功
git remote -v
3. 创建分支接受仓库B代码
git checkout -b 仓库A接受分支名 仓库B别名/仓库B分支名
git checkout -b gitlab-code-branch gitlabRepo/testTwo
4. 将仓库B代码下载到本地(不明白,下载到本地哪?3,4谁在前谁在后)
git fetch 仓库B别名
git fetch gitlabRepo
5. 切回需要合并的分支
git checkout 仓库A分支
git checkout dev
6. 将【仓库A接受分支】合并到【仓库A某分支】
git merge 仓库A接受分支名
git merge gitlab-code-branch --allow-unrelated-histories
场景2: 基于场景1, 仓库A又更新了需要再合并
git checkout gitlab-code-branch
git pull
git checkout dev
git merge gitlab-code-branch
注意:如果仓库A>仓库B,则合并不生效(其实也不是不生效,是认为你已经基于要合并的代码改动过了)