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

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

问题描述

我刚刚开始使用 GIT 并认为它很棒,但是我对 merge 命令的作用有点困惑.

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

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

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

我回家并对该分支进行更改并将其保存为B".另一个程序员对A"进行了更改并将其保存为C".

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".

有没有办法将两个分支B"和C"合并在一起,然后将更改作为新分支提交,比如D"?

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 用于将两个(或多个)分支合并在一起.

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

一个小例子:

# 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"

所以现在有三个独立的分支(即 A B 和 C),具有不同的头部

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

要将更改从 B 和 C 返回到 A,请签出 A(在此示例中已完成),然后使用合并命令:

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---/

如果您想跨存储库/计算机边界合并,请查看 git pull 命令,例如从带有分支 A 的 pc(本示例将创建两个新提交):

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天全站免登陆