git容许两个开发人员在任什么时候候合并修改,这一切并不须要一个中央仓库。一个合并会结合两个或者多个历史提交分支。尽管git支持同时合并三个、四个或者多个分支,但大多数状况下,一次合并只结合两个分支。git
git中的合并,必须发生在一个版本库中,就是所要进行的合并必须在一个版本库中。code
当一个分支的修改和另外一个分支的修改不发生冲突的时候,git会计算合并结果,并建立一个新的提交来表明统一的状态。当分支冲突时,git并不解决冲突,冲突一般出如今对同一个文件的同一行进行修改的时候。git会把这种有争议的修改在索引中标记为未合并(unmerged),留给开发人员处理。索引
为了把other_branch
合并到branch
中,应该检出branch
,并把other_branch
分支合并到branch中。以下所示:开发
$ git checkout branch $ git merge other_branch
下面的例子,建立一个只有一个文件的版本库,而后建立两个分支,再把这两个分支合并在一块儿。it
$ git init Initialized empty Git repository in /Users/iEpac/Documents/git/merge_test/.git/ $ git config user.email 'iepac4srv@163.com' $ git config user.name 'jEpac'
$ cat > file Line 1 stuff Line 2 stuff Line 3 stuff $ git add file $ git commit -m "init 3 line file" [master (root-commit) f391dda] init 3 line file 1 file changed, 3 insertions(+) create mode 100644 file
在master分支上建立另外一个提交:io
$ cat > other_file here is stuff on another file $ git add other_file $ git commit -m "another file" [master 5cb5fea] another file 1 file changed, 1 insertion(+) create mode 100644 other_file
到如今为止,版本库中已经有了两个提交的master分支,每次提交都建立了一个新文件。而后,切换到另个分支,修改第一个文件:ast
$ git checkout -b alternate master^ Switched to a new branch 'alternate' $ git branch * alternate master $ git show-branch * [alternate] init 3 line file ! [master] another file -- + [master] another file *+ [alternate] init 3 line file
alternate
分支是从master^分支派生来的。接下来,对文件作一些修改以便有内容来合并。test
$ cat >> file init 4 alternate stuff $ git add file $ git commit -m 'add alternate`s line 4' [alternate 3985336] add alternate`s line 4 1 file changed, 1 insertion(+)
如今已经有了两个分支,master和alternate,每一个分支都有不一样的开发工做:email
注意:git merge
是区分上下文的。file
当前分支始终是目标分支
因此,alternate
分支应该合并到mater
分支中。因此,在合并前必需要切到master分支
上。
$ git checkout master Switched to branch 'master' $ git show-branch ! [alternate] add alternate`s line 4 * [master] another file -- + [alternate] add alternate`s line 4 * [master] another file +* [alternate^] init 3 line file $ git status On branch master nothing to commit, working directory clean
$ git merge alternate Merge made by the 'recursive' strategy. file | 1 + 1 file changed, 1 insertion(+) $ git ls-files file other_file $ git show-branch ! [alternate] add alternate`s line 4 * [master] Merge branch 'alternate' -- - [master] Merge branch 'alternate' +* [alternate] add alternate`s line 4
可使用提交图看一下:
$ git log --graph --pretty=oneline --abbrev-commit * 37b02aa Merge branch 'alternate' |\ | * 3985336 add alternate`s line 4 * | 5cb5fea another file |/ * f391dda init 3 line file
f391dda
提交处分开5cb5fea
和3985336
37b02aa
处合并$ git merge alternate 自动合并 file 冲突(内容):合并冲突于 file 自动合并失败,修正冲忽然后提交修正的结果
当合并出现冲突的时候, 首先要使用git diff查看文件的冲突程度
# git diff file diff --cc file index 39b21ea,a892d57..0000000 --- a/file +++ b/file @@@ -2,5 -2,5 +2,10 @@@ line 1 stuf line 2 stuff line 3 stuff line 4 alternate stuff ++<<<<<<< HEAD +line 5 stuff +line 6 stuff ++======= + line 5 alternate stuff + line 6 alternate stuff ++>>>>>>> alternate
这时候须要手动解决冲突, 而后git add git commit。从新提交修改过的版本。
有冲突的修改不能自动合并
git会对有问题的文件进行追踪,并在索引中把他们标记为冲突的(conflicted)或者未合并(unmerged)的。使用以下命令来查看工做目录中仍然未合并的一组文件
git status git ls-files -u
移除冲突标记,而后执行git add和git commit. 下面是git提供的解决冲突。
对冲突使用git diff
有冲突的文件合并后, 工做目录中的这个文件内容被修改成冲突后的合并内容,并有冲突标识.
对冲突使用git log命令
git log --merge --left-right -p
不要对有冲突标记的文件执行git add命令, 这会清除索引中的冲突并容许提交, 可是文件是错误的