在GIT中合并2个分支 [英] Merging 2 branches together in GIT

查看:112
本文介绍了在GIT中合并2个分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是刚开始使用GIT并认为它很棒,但是我对 merge 命令的功能有些困惑。



假设我们在分支A中有一个工作项目。



我回家并对此分支进行更改并保存它作为B。
另一位程序员对A进行了修改并将其保存为C。

是否有合并两个分支B和C的方法一起,然后提交更改为新分支,说D?

使用合并两个(或更多)分支。



一个小例子:

 #分支A:
#创建新分支B
$ git checkout -b B
#hack hack
$ git commit -am在分支B上提交

#从A $ b $创建新分支C b $ git checkout -b CA
#hack hack
$ git commit -am在分支C上提交

#返回分支
$ git checkout A
#hack hack
$ git commit -am在分支A上提交



<所以现在有三个不同的分支(即AB和C),它们具有不同的头部以将从B和C的变化返回到A,结账A(alre然后使用合并命令:

 #创建章鱼合并
$ git merge BC

您的历史记录将如下所示:

  ... -oox ------- A 
| \ / |
| B --- / |
\ /
C --- /

如果您想合并跨存储库/计算机边界,看看 git pull 命令,例如从分支A的PC(这个例子将创建两个新的提交):

 #pull branch B 
$ git拉ssh:// host / ... B
#拉分支C
$ git拉ssh://主机/ ... C


I've only just started to use GIT and think its wonderful, however I'm a little confused over what the merge command does.

Let us say we have a working project in the branch "A".

I go home and make changes to this branch and save it as "B". Another programmer makes changes to "A" and saves it as "C".

Is there a way to merge the two branches "B" and "C" together, then commit the changes as a new branch, say "D"?

Or am missing the point of 'merge'?

解决方案

merge is used to bring two (or more) branches together.

a little example:

# on branch A:
# create new branch B
$ git checkout -b B
# hack hack
$ git commit -am "commit on branch B"

# create new branch C from A
$ git checkout -b C A
# hack hack
$ git commit -am "commit on branch C"

# go back to branch A
$ git checkout A
# hack hack
$ git commit -am "commit on branch A"

so now there are three separate branches (namely A B and C) with different heads

to get the changes from B and C back to A, checkout A (already done in this example) and then use the merge command:

# create an octopus merge
$ git merge B C

your history will then look something like this:

…-o-o-x-------A
      |\     /|
      | B---/ |
       \     /
        C---/

if you want to merge across repository/computer borders, have a look at git pull command, e.g. from the pc with branch A (this example will create two new commits):

# pull branch B
$ git pull ssh://host/… B
# pull branch C
$ git pull ssh://host/… C

这篇关于在GIT中合并2个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆