在 GIT 中合并 2 个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3404294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:51:18  来源:igfitidea点击:

Merging 2 branches together in GIT

gitmergecommit

提问by dotty

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

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

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

假设我们在分支“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".

我回家并对该分支进行更改并将其保存为“B”。另一位程序员对“A”进行更改并将其保存为“C”。

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

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

Or am missing the point of 'merge'?

或者我错过了“合并”的重点?

回答by knittl

mergeis used to bring two (or more) branches together.

merge用于将两个(或多个)分支放在一起。

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

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

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

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

# 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 pullcommand, e.g. from the pc with branch A (this example will create two new commits):

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

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

回答by Mohamed Shaban

If you want to merge changes in SubBranchto MainBranch

如果要将SubBranch 中的更改合并到MainBranch

  1. you should be on MainBranch git checkout MainBranch
  2. then run merge command git merge SubBranch
  1. 你应该在 MainBranch git checkout MainBranch
  2. 然后运行合并命令 git merge SubBranch

回答by kisHoR

Case: If you need to ignore the merge commit created by default, follow these steps.

案例:如果需要忽略默认创建的合并提交,请按照以下步骤操作。

Say, a new feature branch is checked out from master having 2 commits already,

比如说,一个新的功能分支是从已经有 2 个提交的 master 中检出的,

  • "Added A" , "Added B"
  • “添加A”,“添加B”

Checkout a new feature_branch

Checkout a new feature_branch

  • "Added C" , "Added D"
  • “加C”,“加D”

Feature branch then adds two commits-->

功能分支然后添加两个提交-->

  • "Added E", "Added F"
  • “添加E”,“添加F”

enter image description here

在此处输入图片说明

Now if you want to merge feature_branch changes to master, Do git merge feature_branchsitting on the master.

现在,如果您想将 feature_branch 更改合并到 master,请git merge feature_branch坐在 master 上。

This will add all commits into master branch (4 in master + 2 in feature_branch = total 6) + an extra merge commit something like 'Merge branch 'feature_branch'' as the master is diverged.

这会将所有提交添加到 master 分支(master 中的 4 + feature_branch 中的 2 = 总计 6)+ 一个额外的合并提交,例如'Merge branch 'feature_branch'' as the master is diverged

If you really need to ignore these commits (those made in FB) and add the whole changes made in feature_branch as a single commit like 'Integrated feature branch changes into master', Run git merge feature_merge --no-commit.

如果您真的需要忽略这些提交(在 FB 中进行的提交)并将在 feature_branch 中所做的整个更改添加为单个提交,例如'Integrated feature branch changes into master'Run git merge feature_merge --no-commit

With --no-commit, it perform the merge and stop just before creating a merge commit, We will have all the added changes in feature branch now in master and get a chance to create a new commit as our own.

使用 --no-commit,它会在创建合并提交之前执行合并并停止,我们现在将在主分支中添加所有添加的更改,并有机会创建一个新的提交作为我们自己的提交。

Read here for more : https://git-scm.com/docs/git-merge

在这里阅读更多信息:https: //git-scm.com/docs/git-merge