git -处理分支合并

1.分支间的合并

1)直接合并:把两个分支上的历史轨迹合二为一(就是所以修改都全部合并)

zhangshuli@zhangshuli-MS-7817:~/myGit$ vim merge.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
g# On branch thrid
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    merge.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git add .
zhangshuli@zhangshuli-MS-7817:~/myGit$ git commit -m "merge"
[thrid 6436f0b] merge
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 merge.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git log -1
commit 6436f0b6bbe0da18ce65e90dd7ea90cf4008bbe3
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Sat Feb 28 12:37:47 2015 +0000

    merge
zhangshuli@zhangshuli-MS-7817:~/myGit$ git checkout parent 
Switched to branch 'parent'
zhangshuli@zhangshuli-MS-7817:~/myGit$ git merge thrid 
Merge made by recursive.
 merge.txt         |    1 +
 mysite-1.0.tar.gz |  Bin 0 -> 501 bytes
 mysite-1.0.zip    |  Bin 0 -> 501 bytes
 3 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 merge.txt
 create mode 100644 mysite-1.0.tar.gz
 create mode 100644 mysite-1.0.zip
zhangshuli@zhangshuli-MS-7817:~/myGit$ git br -av
* parent 44d288e Merge branch 'thrid' into parent
  son    3a1cbb4 ccc add
  thrid  6436f0b merge
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt  bbb.txt  ccc.txt  ddd.txt  merge.txt  mysite-1.0.tar.gz  mysite-1.0.zip
zhangshuli@zhangshuli-MS-7817:~/myGit$ git log -1
commit 44d288e4316e47ac9b6e3ee6a4152aceff5d17ce
Merge: e2e09c4 6436f0b
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Sat Feb 28 12:38:07 2015 +0000

    Merge branch 'thrid' into parent
zhangshuli@zhangshuli-MS-7817:~/myGit$ 

2)压合合并:一分支若干提交合并为一个,然后合并到另外的分支上(squash)

zhangshuli@zhangshuli-MS-7817:~/myGit$ git branch
  parent
  son
* third
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt  bbb.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim ddd.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch third
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    ccc.txt
#    ddd.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git add ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git commit -m "commit ccc.txt"
[third 6a74382] commit ccc.txt
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git log -1
commit 6a74382c9c481515cd70136ce3212ace7aa1c50e
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Sat Feb 28 14:08:24 2015 +0000

    commit ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch third
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    ddd.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git add .
zhangshuli@zhangshuli-MS-7817:~/myGit$ git commit -m "commit ddd"
[third 618011f] commit ddd
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 ddd.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git log -1
commit 618011fc23e6fe8108e830e2812a298ad217d0cb
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Sat Feb 28 14:08:45 2015 +0000

    commit ddd
zhangshuli@zhangshuli-MS-7817:~/myGit$ git checkout parent 
Switched to branch 'parent'
zhangshuli@zhangshuli-MS-7817:~/myGit$ git merge --squash third 
Updating 2a30fca..618011f
Fast-forward
Squash commit -- not updating HEAD
 ccc.txt |    1 +
 ddd.txt |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 ccc.txt
 create mode 100644 ddd.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch parent
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   ccc.txt
#    new file:   ddd.txt
#
zhangshuli@zhangshuli-MS-7817:~/myGit$ git commit -m "yahe"
[parent 02906a1] yahe
 2 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 ccc.txt
 create mode 100644 ddd.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git log -1
commit 02906a15dc15be5fefd5e41dcd3977bb2f762cff
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Sat Feb 28 14:10:45 2015 +0000

    yahe
zhangshuli@zhangshuli-MS-7817:~/myGit$ 

从上面我们可以看出来,所谓的压合分支,其实也是把另一个分支的状态全部移到另一个分支上,只是它并不是merge,而是把提交转化为变更存储到目标分支缓存,让你统一处理

3)捡选合并(cherry-picking):把某个提交进行合并

zhangshuli@zhangshuli-MS-7817:~/myGit$ git log -1
commit 068698c5c848ded943f3454691f420136a2930ee
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Sat Feb 28 14:20:25 2015 +0000

    cherry-pick
zhangshuli@zhangshuli-MS-7817:~/myGit$ git checkout parent 
Switched to branch 'parent'
zhangshuli@zhangshuli-MS-7817:~/myGit$ git cherry-pick 068698c5c848ded943f3454691f420136a2930ee
Finished one cherry-pick.
[parent 1c33f51] cherry-pick
 1 files changed, 1 insertions(+), 0 deletions(-)
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt  bbb.txt  ccc.txt  ddd.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git log -1
commit 1c33f513af17f21dd157522b98a2027047d599c3
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date:   Sat Feb 28 14:20:25 2015 +0000

    cherry-pick
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt  bbb.txt  ccc.txt  ddd.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim aaa.txt 
zhangshuli@zhangshuli-MS-7817:~/myGit$

它使用的命令是cherry-pick 而且参数已经不是branch 而是节点号了

如果你想cherry-pick某个提交,但是又不立刻把这个提交在本分支上提交,你可以使用参数-n 它的作用就和压合合并效果类似

zhangshuli@zhangshuli-MS-7817:~/myGit$ git cherry-pick -n 068698c5c848ded943f3454691f420136a2930ee
Finished one cherry-pick.
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st 
# On branch parent
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   aaa.txt
#
zhangshuli@zhangshuli-MS-7817:~/myGit$ git commit

2.合并分支冲突的处理

这时候只要执行命令git mergetool就行了,它会自动打开一个merge工具

posted @ 2015-02-28 14:50  小白屋  阅读(419)  评论(0编辑  收藏  举报