Git
git cherry-pick
Learn to pick a single commit with git cherry-pick.
What is git cherry-pick?
The command that takes a single commit and applies it to the branch you are on is git cherry-pick. This way you move only the change you picked instead of merging the whole branch. You give the command the hash of the commit to move, and the change is applied to the current branch.
git cherry-pick a1b2c3d
The original commit stays on its own branch.
A new commit is created
When cherry-pick runs, a new commit is created on the target branch while the original commit stays on its own branch. The hash of the new commit is different from the hash of the commit you moved.
git log --oneline
9e4f2ab ozellik eklendi
3c1d7aa temel kayit
The changes are copied, but the commit identity is produced again.
Exercise Take one commit from a branch
Create the temel.txt file, add it and save quietly. Open a branch named ozellik; there create yeni.txt with ozellik inside, add it and save quietly. Go back to the previous branch, take the commit on the ozellik branch with git cherry-pick, and print the contents of yeni.txt at the end.
Show example solution
git init -q
echo temel > temel.txt
git add temel.txt
git commit -q -m "temel"
git checkout -q -b ozellik
echo ozellik > yeni.txt
git add yeni.txt
git commit -q -m "ozellik"
git checkout -q -
git cherry-pick ozellik >/dev/null 2>&1
cat yeni.txt
Test yourself
-
What does the git cherry-pick command do?
- It resolves any conflicts automatically
- It merges the whole history of a branch into the current one
- It deletes the given commit from the branch
- It applies a single commit from another branch to the current one
Show answer
It applies a single commit from another branch to the current one — cherry-pick picks a single commit and applies it to the current branch; it does not merge the whole branch, delete commits, or resolve conflicts on its own.
-
Write the command
git checkout -
git ____ ozellik
Show answer
git checkout -
git cherry-pick ozellik
-
Cherry-pick basics
- git cherry-pick
- Applies a single commit
- single commit
- One change record
- apply
- Carries the change over
- new commit
- A new record on the target
Next lesson: Picking a single commit