Git cherry-pick
git cherry-pick is yet another great feature of git.With the help of this command we can apply the changes in a branch, introduced by an existing commit in some other branch. So, it allows to copy commits from one branch to another, but one commit at a time.
Syntax for this command is :- git cherry-pick [–edit] [-n] [-m parent-number] [-x] <commit>
Lets take an example :- Considering two branches master and Feature1 .Now the branch Feature1 had implemented some features,and all these features are not to be pushed to master for now .Lets suppose at this time master requires a single feature from Feature1 branch not all the features.So here the Feature1 branch is not required to be merged to master branch and all the features on Feature1 branch to be applied on master branch, but it requires cherry-picking a single commit from Feature1 branch on branch master.
Step 1:- See the logs of branch Feature1
[java]
$ git checkout Feature1
Switched to branch ‘Feature1’
$ git log -12 –oneline –decorate –graph
* ebf6dc5 (HEAD, Feature1) updated movie/show page and movieReview page so that critic reviews open in a new tab
* 1913561 Implemented A as a new critic rating source
* bfdb04f Implemented B as a new critic rating source
* b173d91 Implemented C as a new critic rating source
* dc72a67 updated criticRating url finder for source D
* 475be65 Merge branch ‘master’ of github.com:IntelliGrape/RATOR into Feature1
|\
| * 6469926 (master) Merge branch ‘master’ of github.com:IntelliGrape/RATOR
| |\
| * | d01398a resolved problems in critic rating url finder for sources F and G
| * | 62133e9 Merge branch ‘master’ of github.com:IntelliGrape/RATOR
| |\ \
| * | | 6f614ee resoved conflict
* | | | 76b6da1 Implemented E as a new critic rating source
| |_|/
|/| |
* | | 789cbd8 (origin/master, origin/Feature1) updated critic rating scraper for source D
| |/
[/java]
$ git checkout Feature1
Switched to branch ‘Feature1’
$ git log -12 –oneline –decorate –graph
* ebf6dc5 (HEAD, Feature1) updated movie/show page and movieReview page so that critic reviews open in a new tab
* 1913561 Implemented A as a new critic rating source
* bfdb04f Implemented B as a new critic rating source
* b173d91 Implemented C as a new critic rating source
* dc72a67 updated criticRating url finder for source D
* 475be65 Merge branch ‘master’ of github.com:IntelliGrape/RATOR into Feature1
|\
| * 6469926 (master) Merge branch ‘master’ of github.com:IntelliGrape/RATOR
| |\
| * | d01398a resolved problems in critic rating url finder for sources F and G
| * | 62133e9 Merge branch ‘master’ of github.com:IntelliGrape/RATOR
| |\ \
| * | | 6f614ee resoved conflict
* | | | 76b6da1 Implemented E as a new critic rating source
| |_|/
|/| |
* | | 789cbd8 (origin/master, origin/Feature1) updated critic rating scraper for source D
| |/
[/java]
Here we find branch Feature1 is at commit “ebf6dc5” and master is at commit “789cbd8”
Step 2:- Checkout to master branch and look at its log.
[java]
$ git checkout master
Switched to branch ‘master’
$ git log -8 –oneline –decorate –graph
* 6469926 (HEAD, master) Merge branch ‘master’ of github.com:IntelliGrape/RATOR
|\
| * 789cbd8 (origin/master, origin/Feature1) updated glamSham critic rating scraper
* | d01398a resolved problems in critic rating url finder for sources F and G
* | 62133e9 Merge branch ‘master’ of github.com:IntelliGrape/RATOR
|\ \
| |/
| * 174a6b6 (Feature2) resolved conflicts when pulled frm master
| |\
| | * 99e3b0b Resolved conflict
| | |\
| | * | 9cf6965 Added another config for www
| | * | d241d62 updated www config
$ git checkout master
Switched to branch ‘master’
$ git log -8 –oneline –decorate –graph
* 6469926 (HEAD, master) Merge branch ‘master’ of github.com:IntelliGrape/RATOR
|\
| * 789cbd8 (origin/master, origin/Feature1) updated glamSham critic rating scraper
* | d01398a resolved problems in critic rating url finder for sources F and G
* | 62133e9 Merge branch ‘master’ of github.com:IntelliGrape/RATOR
|\ \
| |/
| * 174a6b6 (Feature2) resolved conflicts when pulled frm master
| |\
| | * 99e3b0b Resolved conflict
| | |\
| | * | 9cf6965 Added another config for www
| | * | d241d62 updated www config
[/java]
Step 3:- We have to cherry-pick the commit “dc72a67” from branch Feature1 and apply the changes to master branch.
[java]
$ git cherry-pick dc72a67
Finished one cherry-pick.
[master de7a70a] updated criticRating url finder for source D
1 files changed, 2 insertions(+), 1 deletions(-)
$ git cherry-pick dc72a67
Finished one cherry-pick.
[master de7a70a] updated criticRating url finder for source D
1 files changed, 2 insertions(+), 1 deletions(-)
[/java]
Step 4:- Now again lets see the logs of branch master to see the changes of cherry-picking.
[java]
$ git log -8 –oneline –decorate –graph
* de7a70a (HEAD, master) updated criticRating url finder for source D
* 6469926 Merge branch ‘master’ of github.com:IntelliGrape/RATOR
|\
| * 789cbd8 (origin/master, origin/bollywoodHungama) updated glamSham critic rating scraper
* | d01398a resolved problems in ibn and indian express critic rating url finder
* | 62133e9 Merge branch ‘master’ of github.com:IntelliGrape/RATOR
|\ \
| |/
| * 174a6b6 (glamSham) resolved conflicts when pulled frm master
| |\
| | * 99e3b0b Resolved conflict
| | |\
| | * | 9cf6965 Added another config for www
$ git log -8 –oneline –decorate –graph
* de7a70a (HEAD, master) updated criticRating url finder for source D
* 6469926 Merge branch ‘master’ of github.com:IntelliGrape/RATOR
|\
| * 789cbd8 (origin/master, origin/bollywoodHungama) updated glamSham critic rating scraper
* | d01398a resolved problems in ibn and indian express critic rating url finder
* | 62133e9 Merge branch ‘master’ of github.com:IntelliGrape/RATOR
|\ \
| |/
| * 174a6b6 (glamSham) resolved conflicts when pulled frm master
| |\
| | * 99e3b0b Resolved conflict
| | |\
| | * | 9cf6965 Added another config for www
[/java]
So,here we see that the changes of commit “de7a70a” is applied on master branch.
Hope this will help the git users. 🙂
Regards
Shweta Gupta
Intelligrape
This is pretty nice Shweta. Thanks – I was looking out for something similar!