CodeBlanks Logo Icon CodeBlanks

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.

Practice 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.

Starter code

terminal
git init -q
# 1) temel.txt olustur, ekle ve sessizce kaydet
# 2) ozellik adinda yeni bir dal ac
# 3) yeni.txt olustur, icine ozellik yaz, ekle ve sessizce kaydet
# 4) onceki dala don
# 5) ozellik dalindaki commit'i cherry-pick ile al
# 6) yeni.txt icerigini yazdir

Sample solution

terminal
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

Expected output

ozellik

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 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 Correct answer

Explanation: 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.

2 Write the command

Question
git checkout -
git ____ ozellik
Answer
git checkout -
git cherry-pick ozellik

Explanation: The subcommand that applies a single commit is cherry-pick; there is no cherry, pick or take subcommand.

3 Cherry-pick basics

Term Meaning
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

All lessons in Intermediate

20 Intermediate final

  1. Branch management
  2. Rebase
  3. Conflict
  4. PR flow