文档章节

Cherry Picking with Git

Kolosek
 Kolosek
发布于 2018/06/25 20:10
字数 634
阅读 2
收藏 0

Managing feature branches changes that aren't quite ready for a full merge can be a difficult task. Sometimes you don't want to push a whole branch into another, and only need to pick a few specific commits. This process is called cherry-picking.

Why Cherry Pick?

Let's take a look at the following scenario which will make it easier to understand how cherry-picking works.

You are implementing new features for the next weekly sprint. Once the code is ready, you will push it to the development branch and open it up on the next public release when everything has been tested out.

But, the client isn't pleased with all of the changes and is asking you to introduce only specific ones. Because not all features have been accepted for the next release, a git merge or git rebase would not produce the desired results since it will include all changes made in the last sprint.

Cherry pick is the right answer! It focuses only on the changes included in the commit that implement the wished features without bringing along other commits.

There are many other uses that this tool can offer:

  • It is essential when it comes to bug fixing because bugs are fixed and tested in the development branch with their individual commits.
  • You can avoid unnecessary conflicts by using git cherry-pick instead of other options that as well apply changes from the given commits, such as git diff.
  • A useful tool whenever a full branch merge is not possible due to incompatible versions in the various branches.
  • git cherry-pick is used to get the changes introduced to a sub-branch, without changing the branch.

How to Cherry Pick?

Let's cherry-pick a commit from the development and add it to feature branch by using the commit hash.

git-cherry-pick

Let’s say you want to pick a commit C from the master. There are a few steps you should follow to get the job done:

  1. Obtain the commit hash. You can do this in two ways:
    • By typing git log --oneline, to get the log of your commits history. Make sure you are on the correct branch: git checkout master.
    • Selecting the commit hash from the GitHub page.
  2. Checkout to the branch that you want to insert the commit into, in your case this is the feature branch: git checkout feature.
  3. Cherry-pick the commit: git cherry-pick C.

git-cherry-pick-1

If you run the git log you will see that your cherry-picked commit at the top of the feature branch. It will have a new and different commit hash.

Tips and Tricks

  • If you want to select more than one commit at once, you can add their commit hashes separated by a space: git cherry-pick C D.
  • You can cherry-pick a merge instead of a commit: git cherry-pick -m 1 <merge_hash>. ==Be careful!== It is preferred to use git merge instead of git cherry-pick. When you cherry-pick a merge commit, it collapses all the changes made into that one commit. You lose the commit history.

Be wary of cherry-picking a lot of commits out of order. The new sequence will be reflecting the way you cherry-picked, and not the chronological order of the original commits.

Conclusion

Cherry picking is commonly discouraged in the developer community. The main reason is that it creates duplicate commits and you lose the ability to track the commit history. You should always aim to use git merge.


Merging nearby always overpowers cherry-picking. But it is worth taking a look at this unique Git feature! Share your experience!

This article is originally published at Kolosek Blog.

© 著作权归作者所有

Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
git合并分支上指定的commit

merge 能够胜任平常大部分的合并需求。但也会遇到某些特殊的情况,例如正在开发一个新的功能,线上说有一个紧急的bug要修复。bug修好了但并不像把仍在开发的新功能代码也提交到线上去。这时候...

安小乐
2018/09/13
25
0
github上fork别人的代码之后,如何保持和原作者同步的更新

1.从自己fork之后的版本库clone $ git clone -o chucklu https://github.com/chucklu/Hearthstone-Deck-Tracker.git 参数说明: -o <name> Instead of using the remote name origin to keep......

squanchao
2016/12/15
395
0
TortoiseGit 2.6.0 正式发布,Git 客户端

TortoiseGit 2.6.0 正式发布了。这是与 MsysGit 1.9.5 兼容的最后一个版本(例如,Git for Windows <1.9.5)。 TortoiseGit (海龟Git)是 TortoiseSVN 的 Git 版本,tortoisegit 用于迁移 To...

淡漠悠然
2018/02/22
3.3K
8
Git整理(五) git cherry-pick的使用

概述 git cherry-pick可以理解为”挑拣”提交,它会获取某一个分支的单笔提交,并作为一个新的提交引入到你当前分支上。 当我们需要在本地合入其他分支的提交时,如果我们不想对整个分支进行...

嘿嘿嘿IT
07/16
37
0
git cherry-pick 小结

git cherry-pick可以选择某一个分支中的一个或几个commit(s)来进行操作。例如,假设我们有个稳定版本的分支,叫v2.0,另外还有个开发版本的分支v3.0,我们不能直接把两个分支合并,这样会导致...

鉴客
2018/05/11
517
0

没有更多内容

加载失败,请刷新页面

加载更多

经典系统设计面试题解析:如何设计TinyURL(二)

原文链接:https://www.educative.io/courses/grokking-the-system-design-interview/m2ygV4E81AR 编者注:本文以一道经典的系统设计面试题:《如何设计TinyURL》的参考答案和解析为例,帮助...

APEMESH
28分钟前
7
0
使用logstash同步MySQL数据到ES

概述   在生成业务常有将MySQL数据同步到ES的需求,如果需要很高的定制化,往往需要开发同步程序用于处理数据。但没有特殊业务需求,官方提供的logstash就很有优势了。   在使用logstas...

zxiaofan666
38分钟前
8
0
X-MSG-IM-分布式信令跟踪能力

经过一周多的鏖战, X-MSG-IM的分布式信令跟踪能力已基本具备, 特点是: 实时. 只有要RX/TX就会实时产生信令跟踪事件, 先入kafka, 再入influxdb待查. 同时提供实时sub/pub接口. 完备. 可以完整...

dev5
48分钟前
6
0
OpenJDK之CyclicBarrier

OpenJDK8,本人看的是openJDK。以前就看过,只是经常忘记,所以记录下 图1 CyclicBarrier是Doug Lea在JDK1.5中引入的,作用就不详细描述了,主要有如下俩个方法使用: await()方法,如果当前线...

克虏伯
51分钟前
6
0
实战项目-学成在线(八)

在前后端分离架构中,服务层被拆分成了很多的微服务,微服务的信息如何管理?Spring Cloud中提供服务注册中心来管理微服务信息。 注册中心作用: 1、微服务数量众多,要进行远程调用就需要知...

lianbang_W
52分钟前
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部