文档章节

How to Use Git Merge [the Correct Way]

Kolosek
 Kolosek
发布于 2018/04/30 17:53
字数 625
阅读 9
Git">收藏 0

Isolating features into different branches is a crucial practice for any serious developer. By separating each feature, bugfix or working experiment you will avoid a lot of problems and keep your development branches clean.

At some point, a piece of code will reach a state where you'll want to integrate it with the rest of the project. This is where the git merge command comes in.

Preparing to Merge

Let's assume that we want to merge branch hotfix into your master branch.

Before we start, how to make sure that you are ready to merge your changes?

  1. Check if your local repository is up to date with the latest changes from your remote server with a git fetch.
  2. Once the fetch is completed git checkout master.
  3. Ensure the master branch has the latest updates by executing git pull.
  4. Checkout to the branch that should receive the changes, in our case that is master.

Merging

Once the preparations are completed, you can start the merge with git merge hotfix command.

Fast Forward Merge

fast-forward merge can occur when there is a linear path between branches that we want to merge. If a master has not diverged, instead of creating a new commit, it will just point master to the latest commit of the hotfix branch. All commits from hotfix branch are now available in master.

git-merge-fast-forward

However, a fast-forward merge is not possible if the branches have diverged. In this case, you want to use a Three-way merge.

Three-Way Merge

When there is not a linear path to the target branch, Git has no choice but to combine them via a three-way merge. This merge uses an extra commit to tie together the two branches.

git-merge-three-way-merge-1

Test this out! Create your own project with an RSpec test branch and at the same time edit the Controller tests in master. Now, try to merge.

How to Deal With Merge Conflicts

merge conflict occurs when two branches you're trying to merge both changed the same part of the same file, Git won't be able to figure out which version to use.

For example, if the file example.rb was edited on the same lines in different branches of the same Git repository or if the file was deleted, you will get a merge conflict error when you try to merge these branches. Before you can continue, the merge conflict has to be resolved with a new commit.

Merge conflicts will only occur in the event of a 3-way merge.

  1. Generate a list of the files which need to be resolved: git status
# On branch master
# You have unmerged paths.
#   (fix conflicts and run "git commit")
# Unmerged paths:
#   (use "git add ..." to mark resolution)
# both modified: example.rb
# no changes added to commit (use "git add" and/or "git commit -a")
  1. When the conflicted line is encountered, Git will edit the content of the affected files with visual indicators that mark both sides of the conflicting content. These visual markers are:
    • <<<<<<< - Conflict marker, the conflict starts after this line.
    • ======= - Divides your changes from the changes in the other branch.
    • >>>>>>> - End of the conflicted lines.
<<<<<<< HEAD(master)
conflicted text from HEAD(master)
=======
conflicted text from hotfix
>>>>>>> hotfix
  1. Decide if you want to keep only your hotfix or master changes, or write a completely new code. Delete the conflict markers before merging your changes.
  2. When you're ready to merge, all you have to do is run git add command on the conflicted files to tell Git they're resolved.
  3. Commit your changes with git commit to generate the merge commit.

Hope this helped you get a better understanding how to merge your branches and deal with conflicts.

This article is originally published on Kolosek Blog.

© 著作权归作者所有

Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
A Visual Git Reference

This page gives brief, visual reference for the most common commands in git. Once you know a bit about how git works, it may solidify your understanding. Basic Usage The four co......

SVD
2015/12/05
115
1
git pull 没有想象中的那样怕怕啊-实战解决冲突

NAME git-pull - Fetch from and integrate with another repository or a local branch SYNOPSIS git pull [options] [<repository> [<refspec>…]] DESCRIPTION Incorporates changes fro......

Oscarfff
2016/07/25
4.5K
0
git对比工具beyond compare3配置

For Git 1.7.6 and newer, edit the global config file to set the path: 新版本的配置方式 Diff At a Windows command prompt enter the commands: To launch a diff with BC3, use the co......

龙上
2012/09/26
3.1K
0
git rebase 详细说明

NAME git-rebase - Reapply commits on top of another base tip SYNOPSIS git rebase [-i | --interactive] [options] [--exec <cmd>] [--onto <newbase>][<upstream> [<branch>]]git rebas......

Oscarfff
2016/08/25
239
0
Git v1.7.1 发布

该版本改进内容包括: * Eric Raymond is the maintainer of updated CIAbot scripts, in contrib/. * gitk updates. * Some commands (e.g. svn and http interfaces) that interactively a......

红薯
2010/05/05
681
0

没有更多内容

加载失败,请刷新页面

加载更多

Rust:最小化窗口后 CPU占用率高 (winit,glutin,imgui-rust)

最近试着用 imgui-rust 绘制界面,发现窗口最小化后CPU占用会增大。 查询的资料如下: https://github.com/rust-windowing/winit/issues/783 https://github.com/ocornut/imgui/issues/1151 ...

reter
21分钟前
6
0
cloud-zuul路由网关

九、zuul路由网关 概述 1.1 能干嘛 路由、过滤 路由基本配置 路由访问映射规则 十、springCloud config分布式配置中心

榴莲黑芝麻糊
21分钟前
6
0
Circuit Breaker模式

Circuit Breaker模式会处理一些需要一定时间来重连远程服务和远端资源的错误。该模式可以提高一个应用的稳定性和弹性。 问题 在类似于云的分布式环境中,当一个应用需要执行一些访问远程资源...

mskk
34分钟前
9
0
写论文之前的准备都有哪些?干货来了!

原文链接:https://www.lwfdy.com/archives/144.html 之前跟大家谈了许多有关于初稿修改以及写作事项需要注意的问题,那么今天我们来说一说,在写之前,我们需要做哪些准备呢,为了做到下笔如...

辅导员
40分钟前
7
0
idea快捷键

Alt + Enter 引入类 Ctrl + O 查看我们继承的类或者接口中的方法,以及我们要实现的方法 Ctrl + Alt + b 查看接口实现类中方法(就是我们使用接口编程时,在调用实现类方法处直接Ctrl+鼠标左...

行者终成事
48分钟前
10
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部