Pagine

Wednesday 24 May 2017

How to use the Cherry-pick git command

The cherry-pick git command lets you select a commit from a branch to apply it to another. The patch will be considered as a new commit in the selected branch.
The synopsis is:
git cherry-pick <id_commit>

Example

Let's try to understand this by exploring the following example: Developer1 creates the deve1 branch from master and adds a new file in it:
$ touch file.txt
$ git add file.txt
$ git commit -m 'add file.txt'
$ echo "new line" > file.txt
$ git commit -a -m 'updated file.txt'
As you can see, the developer creates the file.txt, adds it into Git, and commits it.Then he edits it and commits again. Now, let's see the commit history for this branch.
$ git log --oneline
5f8ed47 add file.txt
44c67b9 updated file.txt
Now, the developer will apply the first commit to the master branch:
$ git checkout master
$ git cherry-pick 44c67b9 
Let's imagine that the cherry-pick went wrong and the developer wants to abort it:
$ git cherry-pick --abort 
However, if you want to roll back a cherry-pick, you have two ways to do it:
  • If it's in a private branch, you can use the git rebase command.
  • If it's already in a public branch, use the git revert command.

No comments:

Post a Comment