CodeBlanks Logo Icon CodeBlanks

Summary - Cherry-pick

Combine the cherry-pick flow to selectively move commits.

The selective commit move flow

Selective commit moving goes through three steps: you find the hash of the commit to move, go to the target branch, and apply it with git cherry-pick. If a conflict appears you fix the file, mark it with git add and finish with git cherry-pick --continue.

git log --oneline
a1b2c3d ozellik eklendi
7f0c411 temel kayit

git checkout -
git cherry-pick a1b2c3d

In this flow only the commit you chose is moved to the target branch.

Practice Selective commit move

Create dosya.txt with v1 inside, add it and save quietly. On the ozellik branch set dosya.txt to v2 and create ek.txt with ek inside; save both files in one quiet commit and take its hash into the HASH variable. Go back to the previous branch and save dosya.txt with v3. Try to apply the HASH commit with git cherry-pick; resolve the conflict as v3, add the file again and finish with --continue. Print the contents of dosya.txt and ek.txt at the end.

Starter code

terminal
git init -q
# 1) dosya.txt olustur, icine v1 yaz, ekle ve sessizce kaydet
# 2) ozellik adinda yeni bir dal ac
# 3) dosya.txt icine v2 yaz ve ek.txt olustur, icine ek yaz
# 4) iki dosyayi birlikte ekle ve sessizce kaydet
# 5) bu commit'in hash'ini HASH degiskenine kaydet
# 6) uc.txt olustur, ekle ve sessizce kaydet
# 7) onceki dala don
# 8) dosya.txt icine v3 yaz, ekle ve sessizce kaydet
# 9) HASH degiskenindeki commit'i cherry-pick ile dene
# 10) catisma cikinca dosyayi v3 olarak coz ve tekrar ekle
# 11) cherry-pick islemine devam et
# 12) dosya.txt ve ek.txt iceriklerini yazdir

Sample solution

terminal
git init -q
echo v1 > dosya.txt
git add dosya.txt
git commit -q -m "v1"
git checkout -q -b ozellik
echo v2 > dosya.txt
echo ek > ek.txt
git add dosya.txt ek.txt
git commit -q -m "degisiklikler"
HASH=$(git rev-parse HEAD)
echo uc > uc.txt
git add uc.txt
git commit -q -m "uc"
git checkout -q -
echo v3 > dosya.txt
git add dosya.txt
git commit -q -m "v3"
git cherry-pick $HASH >/dev/null 2>&1 || true
echo v3 > dosya.txt
git add dosya.txt
GIT_EDITOR=true git cherry-pick --continue >/dev/null 2>&1
cat dosya.txt
cat ek.txt

Expected output

v3
ek

Questions and answers

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

1 Which command moves only the commit you chose to the current branch?

  • git stash pop
  • git cherry-pick a1b2c3d Correct answer
  • git reset --hard
  • git init

Explanation: git cherry-pick applies the single commit you name by hash to the current branch; reset, stash and init do not do this job.

2 Put the hash in the command

Question
git checkout -
git cherry-pick a1b2c3d____
Answer
git checkout -
git cherry-pick a1b2c3da1b2c3d

Explanation: In the log output of the card, the line ozellik eklendi has the hash a1b2c3d; cherry-pick wants exactly that hash.

3 Selective move flow

Term Meaning
commit_hash Selects the commit to move
git cherry-pick Applies the chosen commit
conflict A state waiting for a fix
git cherry-pick --continue Continues after the fix

All lessons in Intermediate

20 Intermediate final

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