CodeBlanks Logo Icon CodeBlanks

Picking a single commit

Learn to pick a single commit from another branch.

Choosing with a commit hash

Every commit has a unique hash value. The git log --oneline command lists commits with their hashes on single lines, and this is where you take the hash to give cherry-pick. git rev-parse HEAD gives the hash of the current commit directly.

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

This is how the commit to move is picked out.

One commit from another branch

To take a single commit from another branch you first take its hash and then apply it to the current branch. If you keep the hash in a variable it does not get lost between the two commands; git checkout - returns to the previous branch.

HASH=$(git rev-parse HEAD)
git checkout -
git cherry-pick $HASH

This way only the commit you chose is moved.

Practice Move a commit by hash

Create temel.txt, add it and save quietly. Open a branch named gelisme; there create iki.txt with iki inside, add it and save quietly. Store the hash of the HEAD commit in the HASH variable, go back to the previous branch, apply that commit with git cherry-pick, and print the contents of iki.txt.

Starter code

terminal
git init -q
# 1) temel.txt olustur, ekle ve sessizce kaydet
# 2) gelisme adinda yeni bir dal ac
# 3) iki.txt olustur, icine iki yaz, ekle ve sessizce kaydet
# 4) HEAD commit'inin hash'ini HASH degiskenine kaydet
# 5) onceki dala don
# 6) HASH degiskenindeki commit'i cherry-pick ile al
# 7) iki.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 gelisme
echo iki > iki.txt
git add iki.txt
git commit -q -m "iki"
HASH=$(git rev-parse HEAD)
git checkout -q -
git cherry-pick $HASH >/dev/null 2>&1
cat iki.txt

Expected output

iki

Questions and answers

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

1 How does a commit moved by cherry-pick appear on the target branch?

  • It is copied exactly with the same hash number
  • It is removed from the source branch because it moved
  • It appears as a new commit with the same changes Correct answer
  • It is stored only as a file stash

Explanation: cherry-pick applies the changes of the commit and creates a new commit on the target branch; the original commit stays on the source branch.

2 Take the hash

Question
HASH=$(git rev-parse ____)
git checkout -
git cherry-pick $HASH
Answer
HASH=$(git rev-parse HEAD)
git checkout -
git cherry-pick $HASH

Explanation: The reference that gives the hash of the current commit is HEAD; FETCH_HEAD, ORIG_HEAD and BASE_HEAD are different references used for other jobs.

3 Hash tools

Term Meaning
commit hash Unique id of a commit
git log --oneline Lists hashes one per line
git rev-parse HEAD Gives the current hash
git checkout - Returns to the previous branch

All lessons in Intermediate

20 Intermediate final

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