如何从另一个分支'git cherry-pick'到自己的分支
在之前的教程中,我们研究了如何在当前分支上选择落实 ,但是常见的辅助问题之一是如何从另一个分支执行git cherry-pick 。 本教程的重点是如何从另一个分支搅拌樱桃 ,以及该操作的结果如何。
与所有git教程一样,这将从一个干净的存储库和一个空的工作目录开始,这意味着第一步是创建一个新文件夹,我将其命名为git cherry-pic example 。 下一步是从该文件夹中发出git init调用。
/c/ git cherry-pick example (master) $ git init Initialized empty Git repository in C:/_git-cherry-pick-example/.git/
为git cherry-pick准备分支
初始化存储库后,下一步是创建三个新文件,并在创建每个文件后添加提交。 由于回购协议是刚刚初始化的,所有这些都将在master分支上发生。
/c/ git cherry-pick example (master)
$ echo 'abba' > abba.html
$ git add . | git commit -m '1st commit: 1 file'
$ echo 'bowie' > bowie.html
$ git add . | git commit -m '2nd commit: 2 files'
$ echo 'chilliwack' > chilliwack.html
$ git add . | git commit -m '3rd commit: 3 files'
我们将要从另一个分支git cherry-pick ,具体地说,我们将拉入第二个提交,但是在执行此操作之前,我们将删除所有这些文件并执行一次提交以将master分支放回空状态。
/ c / git cherry-pick示例(大师)
$ rm * .html
$ git添加。 | git commit -m'第4次提交:0个文件'
[master d6a8ce2] 4th commit: 0 files
3 files changed, 3 deletions(-)
delete mode 100644 abba.html
delete mode 100644 bowie.html
检查提交历史
发出git reflog命令将显示master分支的丰富提交历史记录。 注意第二个提交的十六进制ID 63162ea,因为这是当我们从另一个分支git cherry-pick时将使用的ID。
/c/ git cherry-pick example (master) $ git reflog d6a8ce2 (HEAD -> master) [email protected]{0}: commit: 4th commit: 0 files bc0f7d1 [email protected]{1}: commit: 3rd commit: 3 files 63162ea [email protected]{2}: commit: 2nd commit: 2 files 6adc6ff [email protected]{3}: commit (initial): 1st commit: 1 file
切换到功能分支
现在,我们将创建开发并将其转移到名为feature的新分支上。
/c/ git cherry-pick example (master) $ git branch feature $ git checkout feature Switched to branch 'feature' /c/ git cherry-pick example (feature)
然后,我们将创建一个名为zip.html的文件并提交此文件,以便在feature分支上创建少量的开发历史记录。
/c/ git cherry-pick example (feature)
$ echo 'zip' > zip.html
$ git add . | git commit -m '1st feature branch commit: 1 file'
下一步是将樱桃从另一个分支摘到这个新分支,但是在我们这样做之前,请考虑一下预期的结果。 我们将从master分支中挑选第二个提交,即创建名为bowie.html的文件的提交。 在另一个分支中,bowie.html文件位于先前创建的abba.html文件旁边。 采摘的樱桃会带回来什么? 它将带回abba.html和bowie.html文件吗? 它会只恢复bowie.html文件吗? 还是在我们尝试跨分支git cherry-pick时命令会失败? 让我们看看发生了什么。
如何跨分支搅动樱桃采摘
bowie.html提交的ID为63162ea,因此git cherry-pick的命令为:
/c/ git cherry-pick example (feature)
$ git cherry-pick 63162ea
[feature d1c9693] 2nd commit: 2 files
Date: Thu May 17 17:02:12 2018 -0400
1 file changed, 1 insertion(+)
create mode 100644 bowie.html
$ ls
bowie.html zip.html
从另一个分支到git cherry-pick的命令输出是将一个文件添加到当前工作树中,即bowie.html文件。 上面发出的目录列表命令显示了两个文件,zip.html文件和bowie.html文件,表明对工作树的唯一更改是添加了第二个文件。
git cherry-pick如何工作
从本示例中可以看到,当您选择樱桃时,返回的内容不是提交发生时分支的整个状态,而是返回发生的提交与git存储库状态之间的增量在樱桃挑选的提交之前。
还应该注意的是,每当您从另一个分支混入cherry-pick时 ,都会在分支历史记录中注册一个新的提交,如以下引用日志所示:
在软件开发周期中,经常需要从另一个分支中摘樱桃 。 从该示例中可以看到,只要知道提交的十六进制ID,从另一个分支执行git cherry-pick是执行的安全且相当简单的功能,尤其是当执行cherry-pick的分支可以合并时进行更改,而不会发生任何冲突或冲突。
进一步提高您的DevOps工具知识
掌握Git? DevOps旅程的下一步是掌握Jenkins CI。 这里有一些很棒的Jenkins教程,它们将带您从初学者到专家。
第1步-如何下载,配置和安装Jenkins教程
第2步-创建您的第一个Jenkins自由式构建作业示例
步骤3 —从Shell脚本中的Jenkins环境变量列表中提取
步骤4 —修复常见的Jenkins插件安装错误
第5步—将String和Boolean Jenkins参数添加到构建中
第6步– 使用Jenkins Git插件从GitHub开始
第7步-当您git硬复位并推动时会发生什么?
第8步-了解詹金斯与Maven的辩论