Git选择性合并

Git
git cherry-pick用于把另一个本地分支的commit修改应用到当前分支。我们通过一个实例来演示怎样使用该指令。假如我们存在master和dev两个分支,我们在dev分支上面commit三次,分别新建对应的文件a.txt、b.txt和c.txt。然后我们进入master分支,将b.txt次的commit应用到master分支上面。

git cherry-pick用于把另一个本地分支的commit修改应用到当前分支。我们通过一个实例来演示怎样使用该指令。

假如我们存在master和dev两个分支,我们在dev分支上面commit三次,分别新建对应的文件a.txt、b.txt和c.txt。然后我们进入master分支,将b.txt次的commit应用到master分支上面。如下:

# 查看当前存在那些分支
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (master)
$ git branch
  dev
* master
 
# 选择dev分支
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (master)
$ git checkout dev
Switched to branch 'dev'
 
# 新建a.txt文件且commit
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ echo "a" > a.txt
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ git add .
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory.
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ git commit -m "new a.txt"
[dev c527058] new a.txt
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
 
# 新建b.txt文件且commit
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ echo "b" > b.txt
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ git add .
warning: LF will be replaced by CRLF in b.txt.
The file will have its original line endings in your working directory.
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ git commit -m "new b.txt"
[dev 481ca57] new b.txt
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt
 
# 新建c.txt文件且commit
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ echo "c" > c.txt
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ git add .
warning: LF will be replaced by CRLF in c.txt.
The file will have its original line endings in your working directory.
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ git commit -m "new c.txt"
[dev f461ff5] new c.txt
 1 file changed, 1 insertion(+)
 create mode 100644 c.txt
 
# 查看git日志
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ git log
commit f461ff538dda1453792f3eef63a0b0b59501f51e     # 第三次提交
Author: huangx <hxstrive@outlook.com>
Date:   Fri Mar 24 13:09:37 2017 +0800
 
    new c.txt
 
commit 481ca5706cd4679b3fa321b13e07e2996b40553f     # 第二次提交
Author: huangx <hxstrive@outlook.com>
Date:   Fri Mar 24 13:09:21 2017 +0800
 
    new b.txt
 
commit c527058bbf3c591efe6252d57d025e480bf49a2e     # 第一次提交
Author: huangx <hxstrive@outlook.com>
Date:   Fri Mar 24 13:09:02 2017 +0800
 
    new a.txt
 
commit e23c966ee690a255c80b441673e066945f52ebf3
Author: huangx <hxstrive@outlook.com>
Date:   Thu Mar 23 22:48:33 2017 +0800
 
# 进入master分支
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ git checkout master
Switched to branch 'master'
 
# 将第二次提交应用到master分支
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (master)
$ git cherry-pick 481ca5706cd4679b3fa321b13e07e2996b40553f
[master e9e9587] new b.txt
 Date: Fri Mar 24 13:09:21 2017 +0800
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt
 
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (master)
$ ls
b.txt  dev.txt  readme.txt  version.txt
 
# 进入dev分支,查看分支文件列表
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (master)
$ git checkout dev
Switched to branch 'dev'
 
Administrator@lenovoPC MINGW32 ~/Desktop/git/git-demo (dev)
$ ls
a.txt  b.txt  c.txt  dev.txt  master.txt  readme.txt  version.txt
睡眠和休息丧失了时间,却取得了明天工作的精力。 —— 毛泽东
0 不喜欢
说说我的看法 -
全部评论(
没有评论
目录
热门标签
热门文章
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号