Hone

Lessons · Git · taking one commit from another branch

Taking one commit

git cherry-pick hash applies that single commit's change onto your current branch as a new commit. For 'I want that one fix over here'.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

A bug fixed on a feature branch is needed on the release branch today; the rest of the feature is not. Cherry-pick moves the fix alone.

How to think about it

Find the hash in git log on the other branch, switch to where you want it, cherry-pick. Expect a new hash: it is a copy. Merge the source branch later and Git usually notices the duplicate.

Worked example

git log --oneline other -3
Find the one fix you want.
git cherry-pick a1b2c3d
Applies that commit's change here as a new commit.
git log --oneline -1
A new hash: same diff, different commit.

Your turn

Bring one commit from another branch.

git cherry-pick 

The trap

A cherry-picked commit is a copy. Merging the source branch later brings the original too; Git usually handles the duplicate, but a conflict there surprises people.

Practise taking one commit from another branch on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.