Thursday, August 18, 2016

Unleashing the Git - part 7 - Cherry picking commits

We looked at git branching in one of my previous post. In this post let's look at git's on of the unique and powerful features that ties with branching; cherry-picking.  

You might need to merge only a specific commit in a branch to your local branch, rather than merging an entire branch. Opt to cherry pick only if merging is not an option. One use case where you want to cherry pick is to back port a bug fix you have done in another branch.

Since we covered some branching concepts in the previous post, let's remind a few commands first.

To see all the available branches
$ git branch
To switch to another branch
$ git checkout <branch name>
Cherry picking a branch, tag or a specific commit could be done using the following command.
$ git cherry-pick <branch name, tag name or commit id>   // In case of branch or tag name, only the latest commit will be picked.

Cherry picking also does the commit. You can avoid the commit with the following. This is important when you want to cherry pick multiple commits and commit all stages at once to commit in the current branch.
$ git cherry-pick --no-commit release_branch_v1 // Or same as $ git cherry-pick --n release_branch_v1
$ git cherry-pick --edit <commit id>   // launches the editor to change te commit message. Same as $ git cherry-pick -e <commit id>
$ git cherry-pick --signoff release_branch_v1   // Adds a “Signed-off-by” line followed by your name and email taken from configuration to the commit message. Same as $ git cherry-pick -s release_branch_v1

No comments:

Post a Comment