
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain Cherry Picking in Git
Cherry picking is a way to choose specific commits from one branch and apply them to another branch. This is useful when you want to select specific changes from a pull request.
git cherry-pick {commit_hash}
The above command will cherry pick the commit associated with the specified commit hash to the current branch.
From the above commits shown in the diagram, we need to apply only commit F1 from the feature branch to the master branch.
In this case, the master branch after cherry picking will look as below.
Example
$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo $ git init Initialized empty Git repository in E:/tut_repo/.git/ $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -m 'hello' [master (root-commit) cac1d77] hello 1 file changed, 1 insertion(+) create mode 100644 hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello 2 >> hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -am 'c2' [master 4f10a5d] c2 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git branch feature $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git switch feature Switched to branch 'feature' $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ echo world>world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git commit -m 'F1' [feature 9b5ddf0] F1 1 file changed, 1 insertion(+) create mode 100644 world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ echo world again>>world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git commit -am 'F2' [feature 629c15d] F2 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git checkout master Switched to branch 'master' $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * 629c15d (feature) F2 * 9b5ddf0 F1 * 4f10a5d (HEAD -> master) c2 * cac1d77 hello $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello3>>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -am 'C3' [master 81cdced] C3 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * 81cdced (HEAD -> master) C3 | * 629c15d (feature) F2 | * 9b5ddf0 F1 |/ * 4f10a5d c2 * cac1d77 hello $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git cherry-pick 9b5ddf0 [master 693bff8] F1 Date: Tue Mar 30 13:43:50 2021 +0530 1 file changed, 1 insertion(+) create mode 100644 world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * 693bff8 (HEAD -> master) F1 * 81cdced C3 | * 629c15d (feature) F2 | * 9b5ddf0 F1 |/ * 4f10a5d c2 * cac1d77 hello $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ ls hello.txt world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ cat world.txt world
Advertisements