Merging Branches: Without a Conflict

Merging Branches: Without a Conflict

Commands discussed in this section:

  • git merge

When To Merge?

Examples of when would you might want to merge branches:

  • You start with a repository you cloned. After you make changes to your local copy of the repository, you will need to fetch/pull new updates from the repository and merge the updates into your local branch.
  • There are multiple branches and you want to merge the multiple branches into your branch.

Starting point

Let’s start with the repository we created in the Creating And Playing With Branches example.

On the master branch there are 3 commits:

  • Commit C1: Created the file README with the contents: This is the README file.
  • Commit C2: Added one more line to the README file: One more line.
  • Commit C3: Created the file Makefile with the contents:
    # Beginnings of a Makefile

The test branch started with the master branch commit C2:

  • Commit T1 created the file plan with the contents: My system test

The below diagram shows the 2 branches and what occurred during each commit:

annotated

Let’s reduce the above diagram to include only the commits along with the master, test and HEAD references:

annotated-reduced

Or we could use a git UI tool like gitg –all to display the commit history for the branches like this:

gitg.png

Merges Go To You

When you do a git merge, the result of the merged branches is placed on your current branch.

The result of the merge is a new commit on the current branch.

For example, if you want git to merge the master and test branches together and have the result placed in the masterbranch, then you would:

$ git checkout master
Switched to branch 'master'
$ ls
README
$ git merge test
Updating 423b692..eac62ab
Fast-forward
 plan |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 plan
$ ls
plan  README

Your commit tree looks like this:

merge

gitg –all shows the commit-tree like this:

merge without a conflict

At this point, the master and test branches have been merged onto the master branch, though you could continue to add commits to either branch. For example, from this point on, your workflow could be any of the following:

  • Continue adding commits to the master branch
  • Checkout the test branch and continue adding commits to the test branch.
  • Stop adding commits to the test branch and adds commits only to the master branch and any other branches you create.
  • etc.

Merging Without A Conflict

If the files from the different branches you’re merging don’t have any conflicts with your current branch, then the merge is done, tweet about the merge, dwell on a merge well done, and take a break.

In the above example, there were no conflicts in the merge of the two branches:

  • git merge test command was done while in
    the master branch.
  • The test branch had a new file named plan that didn’t exist in the master branch.
  • The file plan was copied from the test branch
    to the master branch
  • The merge was finished.

A merge can have no conflicts even if the files on the two branches are different, as long as the differing lines are in different locations of the file.

If you plan to merge with files that never conflict with your files, then you can stop reading about merges, and you can tweet “I’m done with the gitguys merge section!

But there are always conflicts, eventually. So put away your tweeter for now. The good news is there are a lot of tools to help the merge go smoothly.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值