git merge简介(转)
git merge的基本用法为把一个分支或或某个commit的修改合并现在的分支上。
我们可以运行git
merge -h和git merge
--help查看其命令,后者会直接转到一个网页(git的帮助文档),更详细。
usage: git merge [options]
[<commit>...]
or: git merge [options] <msg> HEAD
<commit>
or: git merge --abort
-n do not
show a diffstat at the end of the merge
--stat show a diffstat at the end of the merge
--summary (synonym to --stat)
--log[=<n>] add (at most
<n>) entries from shortlog to merge
commi
t message
--squash create a
single commit instead of doing a merge
--commit perform
a commit if the merge succeeds (default)
-e,
--edit edit message before committing
--ff allow fast-forward
(default)
--ff-only abort if
fast-forward is not possible
--rerere-autoupdate update the index with reused conflict resolution if
po
ssible
-s, --strategy <strategy>
merge strategy to use
-X, --strategy-option <option=value>
option for selected merge
strategy
-m, --message <message>
merge commit message (for a
non-fast-forward merge)
-v, --verbose
be more verbose
-q, --quiet be more
quiet
--abort abort the current
in-progress merge
--progress force
progress reporting
-S, --gpg-sign[=<key
id>]
GPG sign
commit
--overwrite-ignore update ignored
files (default)
git merge [options] <msg> HEAD <commit> 这里的 HEAD 其实就是分支名,用于说明把 HEAD 分支合并到当前分支。
判断是否使用--squash选项最根本的标准是,待合并分支上的历史是否有意义。
resolves as a fast-forward. This is the default behaviour when merging an
annotated (and possibly signed) tag.
切换到Master分支
对Develop分支进行合并
--help查看其命令,后者会直接转到一个网页,才能看到其详细说明
merge),会直接将Master分支指向Develop分支。
使用--no-ff参数后,会执行正常合并,在Master分支上生成一个新节点。为了保证版本演进的清晰,我们希望采用这种做法。关于合并的更多解释,请参考Benjamin
Sandofsky的《Understanding the Git Workflow》。
示图2-1
以下是一篇来自于哈佛大学关于git merge的文章
Merging
After
you have finished implementing a new feature on a branch, you want to bring that
new feature into the main branch, so that everyone can use it. You can do so
with the git
merge or git
pull command.
The
syntax for the commands is as follows:
git merge [head]git pull . [head]
They are identical in result. (Though the merge form seems simpler for now, the reason for the pull form will become apparent when discussing multiple developers.)
These commands perform the following operations. Let the current head be called current, and the head to be merged calledmerge.
- Identify the common ancestor of current and merge. Call it ancestor-commit.
- Deal with the easy cases. If the ancestor-commit equals merge, then do nothing. If ancestor-commit equals current, then do a fast forward merge.
- Otherwise, determine the changes between the ancestor-commit and merge.
- Attempt to merge those changes into the files in current.
- If there were no conflicts, create a new commit, with two parents, current and merge. Set current (and HEAD) to point to this new commit, and update the working files for the project accordingly.
- If there was a conflict, insert appropriate conflict markers and inform the user. No commit is created.
Important note: Git can get very confused if there are uncommitted changes in the files when you ask it to perform a merge. So make sure to commit whatever changes you have made so far before you merge.
So, to complete the above example, say you check out the master head again and finish writing up the new data for your paper. Now you want to bring in those changes you made to the headers.
The repository looks like this:
+---------- (D)
/ |
(A) -- (B) -- (C) -------------- (E)
| |
fix-headers master
|
HEAD
where (E) is the commit reflecting the completed version with the new data.
You would run:
git merge fix-headers
If there are no conflicts, the resulting respository looks like this:
+---------- (D) ---------------+
/ | \
(A) -- (B) -- (C) -------------- (E) -- (F)
| |
fix-headers master
|
HEAD
The merge commit is (F), having parents (D) and (E). Because (B) is the common ancestor between (D) and (E), the files in (F) should contain the changes between (B) and (D), namely the heading fixes, incorporated into the files from (E).
Note on terminology: When I say “merge head A into head B,” I mean that head B is the current head, and you are drawing changes from head A into it. Head B gets updated; nothing is done to head A. (If you replace the word “merge” with the word “pull,” it may make more sense.)
Resolving Conflicts
A conflict arises if the commit to be merged in has a change in one place, and the current commit has a change in the same place. Git has no way of telling which change should take precedence.
To resolve the commit, edit the files to fix the conflicting changes. Then run git add to add the resolved files, and rungit commit to commit the repaired merge. Git remembers that you were in the middle of a merge, so it sets the parents of the commit correctly.
如果没有冲突的话,merge完成。有冲突的话,git会提示那个文件中有冲突,比如有如下冲突:
<<<<<<< HEAD:test.c
printf (“test1″);
=======
printf (“test2″);
>>>>>>> issueFix:test.c
可以看到 ======= 隔开的上半部分,是 HEAD(即 master 分支,在运行 merge 命令时检出的分支)中的内容,下半部分是在 issueFix 分支中的内容。解决冲突的办法无非是二者选其一或者由你亲自整合到一起。比如你可以通过把这段内容替换为下面这样来解决:
printf (“test2″);
这个解决方案各采纳了两个分支中的一部分内容,而且删除了 <<<<<<<,=======,和>>>>>>> 这些行。
在解决了所有文件里的所有冲突后,运行git add 将把它们标记为已解决(resolved)。
然后使用git commit命令进行提交,merge就算完成了
Fast Forward Merges
A fast forward merge is a simple optimization for merging. Say your repository looks like this:
+-- (D) ------ (E)
/ |
(A) -- (B) -- (C) |
| |
current to-merge
|
HEAD
and you run git merge to-merge. In this case, all Git needs to do is set current to point to (E). Since (C) is the common ancestor, there are no changes to actually “merge.”
Hence, the resulting merged repository looks like:
+-- (D) -- (E)
/ |
(A) -- (B) -- (C) |
|
to-merge, current
|
HEAD
That is, to-merge and current both point to commit (E), and HEAD still points to current.
Note an important difference: no new commit object is created for the merge. Git only shifts the head pointers around.
Common Merge Use Patterns
There are two common reasons to merge two branches. The first, as explained above, is to draw the changes from a new feature branch into the main branch.
The second use pattern is to draw the main branch into a feature branch you are developing. This keeps the feature branch up to date with the latest bug fixes and new features added to the main branch. Doing this regularly reduces the risk of creating a conflict when you merge your feature into the main branch.
One disadvantage of doing the above is that your feature branch will end up with a lot of merge commits. An alternative that solves this problem is rebasing, although that comes with problems of its own.
http://blog.csdn.net/hudashi/article/details/7664382
git merge简介(转)的更多相关文章
- git merge简介【转】
转自:http://blog.csdn.net/hudashi/article/details/7664382 git merge的基本用法为把一个分支或或某个commit的修改合并现在的分支上.我们 ...
- git merge简介
git merge的基本用法为把一个分支或或某个commit的修改合并到现在的分支上.我们可以运行git merge -h和git merge --help查看其命令,后者会直接转到一个网页(git的 ...
- git rebase简介(基本篇)
原文: http://gitbook.liuhui998.com/4_2.html 一.基本 git rebase用于把一个分支的修改合并到当前分支. 假设你现在基于远程分支"origin& ...
- git的简介,安装以及使用
1git的简介 Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). Git有什么特点?简单来说就是:高端大气上档次! 2Linus一直痛恨的CVS及SVN都是集中式的版本控制系 ...
- git学习——简介、使用(一)
本文是作者参考其他教程学习git的记录,原文:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c01 ...
- svn与git区别简介,git分支操作在mac客户端soureTree和使用命令行如何实现
svn与git区别简介: 性能方面(经过实践的) svn:下载速度慢,因为它其中的源文件太多,并且在show log日志的时候每次都需要去服务器拉取,速度很慢 git:下载速度快,并且git clon ...
- 【转】git rebase简介(基本篇)
原文网址:http://blog.csdn.net/hudashi/article/details/7664631/ 原文: http://gitbook.liuhui998.com/4_2.html ...
- git merge & git rebase
git merge & git rebase bug error: You have not concluded your merge (MERGE_HEAD exists). hint: P ...
- 聊下git merge --squash
你经常会面临着将dev分支或者很多零散的分支merge到一个公共release分支里. 但是有一种情况是需要你处理的,就是在你的dev的分支里有很多commit记录.而这些commit是无需在rele ...
随机推荐
- 什么是比特币(Bitcoin)?
比特币是一种类型的电子货币.点对点(P2P)网络跟踪和验证交易.比特币系统不涉及金融机构,因此它不需要中央监控单元以控制该货币.它可以利用网络作为现金. 比特币系统 比特币是在处理称为区块(block ...
- Gradle 多渠道打包的使用和错误分析(转)
刚接触到android的开发,对什么都陌生的,本文是自己在项目中使用的技术要点总结,大咖遇到可直接飘过..... 1.Gradle 打包(不废话了直接来脚本),将下列脚本放到build.gradle文 ...
- xp硬盘安装Fedora14 过程记录及心得体会(fedora14 live版本680M 和fedora14 DVD版本3.2G的选择)
这次电脑奔溃了,奇怪的是直接ghost覆盖c盘竟然不中.之前电脑上硬盘安装的fedora14操作系统,也是双系统.不知道是不是这个问题,记得同学说过,在硬盘装fedora之后,要手动修改c盘隐藏的那个 ...
- UVA 12103 - Leonardo's Notebook(数论置换群)
UVA 12103 - Leonardo's Notebook 题目链接 题意:给定一个字母置换B.求是否存在A使得A^2=B 思路:随意一个长为 L 的置换的k次幂,会把自己分裂成gcd(L,k) ...
- 关于委托:异常{ 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型 }
异常{ 无法将 匿名方法 转换为类型"System.Delegate",因为它不是委托类型 } 委托实际上是把方法名作为参数,但是若有好多个方法时,就要指明是哪个参数 查看如下代 ...
- 自定义ViewGroup实现垂直滚动
转载请表明出处:http://write.blog.csdn.net/postedit/23692439 一般进入APP都有欢迎界面,基本都是水平滚动的,今天和大家分享一个垂直滚动的例子. 先来看看效 ...
- 构建工具maven
构建工具maven =UTF-8''Gradle Effective Implementation Guide.pdf: http://www.t00y.com/file/76854506 b ...
- 大约apache 2.4.X虚拟主机配置问题的版本号后,
重装系统,习惯性下载最新的wamp2.5,在各种配置,然后一切正常反应.数据库,代码. 然后打开浏览器,尼嘛,幸运的是,昨天,与虚拟域,其实403该. apache error log的信息是:AH0 ...
- 智能家居DIY
近期智能家居比較火,将房子简单改造下,也算体验智能家居. 本文解说的是用无线的方式,长处是:不用改造现有线路,直接安装模块就可以实现想要的功能,花的钱也较少,共六百左右 =============== ...
- 剖析html对标准标签和自定义标签闭合与不闭合渲染问题
昨天在修改去年写的系统的时候无意中看到了当时写的一个利用标准标签未闭合在单元格内把整个单元格颜色渲染成红色的效果,如下: 当时的问题是从后台返回来的是个int整数而%是写在页面上的如图 这 时候就出现 ...