Git_mergetool_tutorial(转载)
原文链接:https://gist.github.com/karenyyng/f19ff75c60f18b4b8149#table-of-content
Table Of Content
Skip to the relevant sections if needed.
- 2-min tutorial to do it the quick-and-dirty-way
- Concepts for resolving
Git conflicts - Setting up different editors / tool for using
git mergetool mergetoolsimple code example forvimdiff- Other great references and tutorials
Concepts for resolving Git conflicts
For using mergetool in git, we need to understand the following terminology to understand what is being merged:
LOCAL- theheadfor the file(s) from the current branch on the machine that you are using.REMOTE- theheadfor files(s) from a remote location that you are trying to merge into yourLOCALbranch.BASE- the common ancestor(s) ofLOCALandBASE.MERGED- the tag /HEADobject after the merge - this is saved as a new commit.
Common mergetool from editors will display both LOCAL and REMOTE so you can decide which changes to keep. Please read this tutorial explaining the HEAD objects if you do not know what it is. It will help your understanding of Git tremendously.
Setting up different editors / tool for using git mergetool
We have to change the git config to set a default mergetool. In this example, we will use vimdiff:
$ git config merge.tool vimdiff
We can also set the editor to display the common ancestor BASE while we examine what changes are in LOCAL and REMOTE with the following setting:
$ git config merge.conflictstyle diff3
Finding out what mergetool editors are supported
$ git mergetool --tool-help
And we list a few of them:
Command line mergetool editors
GUI mergetool editors
gvimdiff- almost identical tovimdiffbut uses the Linux GUI forVim, please refer tovimdiffif you still use the keyboard commands forGVim.kdiff3meldtortoisemerge
Or consult the community of your favorite editor to see how to do the equivalent operations for your editor.
Other useful mergetool settings
Do not prompt before launching the merge resolution tool
$ git config mergetool.prompt false
mergetool simple code example
creating the git repo
$ mkdir galaxyZoo
$ cd galaxyZoo
$ git init
$ vim astrophy_obj.txt
Add some galaxy types into astrophy_obj.txt then save the file.
# content of astrophy_obj.txt
spiral
ellipitcal
bar
irregular
save then commit the file.
$ git add astrophy_obj.txt
$ git commit -m 'Initial commit'
$ git branch astrophy_objects # create a new branch
$ git checkout astrophy_objects # change to new branch
$ vim astrophy_obj.txt # make changes to file
Change bar to barred in the file.
$ git commit -am 'changed bar to barred'
$ git checkout master # change back to master branch
$ vim astrophy_obj.txt
# add the word `galaxy` to the end of each line using Vim REGEX
# type `:%s/$/ galaxy/g` in Vim then press enter and save `:wq`
$ git commit -am 'added galaxy to each line'
# merge from the astrophy_objects branch to current branch, i.e. master
$ git merge astrophy_objects
Then you will see some error messages:
Auto-merging astrophy_obj.txt
CONFLICT (content): Merge conflict in astrophy_obj.txt
Automatic merge failed; fix conflicts and then commit the result.
We can bring up the mergetool:
$ git mergetool
Then it will bring up the different versions of the file in different Vim splits panels.
+--------------------------------+
| LOCAL | BASE | REMOTE |
+--------------------------------+
| MERGED |
+--------------------------------+
The top left split panel is the LOCAL, top middle split is BASE and top right split is REMOTE. The bottom split refers to the MERGED version. You can find this info in the bottom bar of each split (I have put 3 yellow rectangles to highlight that info).
As you can see form the below image, my Vim has highlighted the differences in red for me.
Now if your terminal has any GUI capability and you have compiled Vim correctly with GUI support, you can use your mouse to click on the bottom split to edit it. Or if you are a Vim ninja, you can use the keyboard shortcut to move to different splits.
Ctrl w + h # move to the split on the left
Ctrl w + j # move to the split below
Ctrl w + k # move to the split on top
Ctrl w + l # move to the split on the right
You can either incorporate the changes by manually editing the MERGED split, or use Vim shortcuts pull from one of the LOCAL, BASE ad REMOTE versions.
:diffg RE # get from REMOTE
:diffg BA # get from BASE
:diffg LO # get from LOCAL
save the changes then quit with :wqa to close all the splits. Remember to commit the merge.
$ git commit -am 'merged from several branches'
Resolving conflict from a git pull
If you were trying to do a git pull when you ran into merge conflicts, follow all steps in the previous section for using the mergetool, then do:
$ git rebase –continue
This command will
Forward-port local commits to the updated upstream HEAD.
according to the documentation, meaning your local commits will be pushed to the upstream remote branch as a new forward commit that doesn't interfere with previous commits. Hooray now you can claim that you can collaborate with others with Git without messing up with your collaborators' commits.
Other vimdiff keyboard shortcuts
]c - Jump to the next change.
[c - Jump to the previous change.
Other great references and tutorials
- Git mergetool documentation on git-scm.com
- Must-read tutorial: the concepts of branching and merging from Charles Duan
- Improving Vimdiff as a Git mergetool
Git_mergetool_tutorial(转载)的更多相关文章
- Crystal Clear Applied: The Seven Properties of Running an Agile Project (转载)
作者Alistair Cockburn, Crystal Clear的7个成功要素,写得挺好. 敏捷方法的关注点,大家可以参考,太激动所以转载了. 原文:http://www.informit.com ...
- RTP与RTCP协议介绍(转载)
RTSP发起/终结流媒体.RTP传输流媒体数据 .RTCP对RTP进行控制,同步.RTP中没有连接的概念,本身并不能为按序传输数据包提供可靠的保证,也不提供流量控制和拥塞控制,这些都由RTCP来负责完 ...
- 《Walking the callstack(转载)》
本文转载自:https://www.codeproject.com/articles/11132/walking-the-callstack Download demo project with so ...
- [转载]MVVM模式原理分析及实践
没有找到很好的MVVM模式介绍文章,简单找了一篇,分享一下.MVVM实现了UI\UE设计师(Expression Blend 4设计界面)和软件工程师的合理分工,在SilverLight.WPF.Wi ...
- [转载]:STM32为什么必须先配置时钟再配置GPIO
转载来源 :http://blog.csdn.net/fushiqianxun/article/details/7926442 [原创]:我来添两句,就是很多同学(包括我)之前搞低端单片机,到了stm ...
- [转载]从MyEclipse到IntelliJ IDEA-让你摆脱鼠标,全键盘操作
从MyEclipse转战到IntelliJ IDEA的经历 注转载址:http://blog.csdn.net/luoweifu/article/details/13985835 我一个朋友写了一篇“ ...
- TCP同步与异步,长连接与短连接【转载】
原文地址:TCP同步与异步,长连接与短连接作者:1984346023 [转载说明:http://zjj1211.blog.51cto.com/1812544/373896 这是今天看到的一篇讲到T ...
- 在CentOS 7/6.5/6.4 中安装Java JDK 8(转载)
转载在CentOS 7/6.5/6.4 中安装Java JDK 8 首先,在你的服务器上运行一下更新. yum update 然后,在您的系统上搜索,任何版本的已安装的JDK组件. rpm -qa | ...
- 用C#实现MD5的加密(转载)
方法一 首先,先简单介绍一下MD5 MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer scien ...
随机推荐
- ESP8266常见问题汇总——转载自官网
ESP8266 常见问题 本页面收集esp8266常见问题 概述 本文档主要介绍开发者在ESP8266开发中常见的一些问题. 这些问题主要包括以下几大类: 基本概念相关 ESP8266 相关 AiCl ...
- pandas聚合和分组运算之groupby
pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对数据集进行切片.切块.摘要等操作.根据一个或多个键(可以是函数.数组或DataFrame列名)拆分pandas对象.计算分 ...
- asp.net大文件传输断点续传源码
HTML部分 <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx. ...
- java中如何根据函数查询引用的jar包
选中函数,按Ctrl+Shift+T,就可以弹出对应的jar包地址 例如:
- (Java多线程系列二)线程间同步
Java多线程间同步 1.什么是线程安全 通过一个案例了解线程安全 案例:需求现在有100张火车票,有两个窗口同时抢火车票,请使用多线程模拟抢票效果. 先来看一个线程不安全的例子 class Sell ...
- JVM之Java类加载机制
什么是类加载机制 虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这既是虚拟机的类加载机制 类的生命周期 生命周期简述 ...
- wpscan
1版本信息检测 WPscan 使用语法 详细参数: --update #更新 -u / --url #要扫描的站点 -f / --force #不检查是否wordpress站点 -e / --enum ...
- ESPCMS的CSRF添加管理员账号
ESPCMS的CSRF添加管理员账号 前言 这里开始的思路是先注入进去 然后getshell 但是不关CSRF什么事 换思路 看了一下群消息 大哥发了一个视频 后台名称admin admin12 ...
- RTS打卡计划第四周
Algorithms: https://leetcode-cn.com/problems/subarray-sum-equals-k/comments/ 此问题开始考虑空间换时间,结果完全不用空间,不 ...
- 学习UEFI 之你把C语言学好了码?学习UEFI 之你把C语言学好了吗?
很多人在问我说: 怎样子把UEFI 学好?! 其实写BIOS 的人答案应该只有一个,把SPCE看懂看完然后融会贯通!这样子的答案好像跟没有是一样的! 小弟就以我的学习经验来分享给大家吧!(虽然我也没学 ...