Git Cherry-pick & Patch
Sometimes you want one commit from another branch, not the whole branch: a bug fix made on a feature branch that is needed in a release right now, or a commit that landed on the wrong branch. git cherry-pick copies the change of a commit onto the current branch as a new commit. Patches do the same across repositories, as files.
Cherry-picking a commit
mkdir app && cd app && git init -q
printf 'version=1.0\ntimeout=30\n' > config.ini && git add . && git commit -q -m "Initial config"
git switch -q -c feature/reports
echo "report=weekly" > reports.ini && git add . && git commit -q -m "Add weekly reports"
sed -i 's/timeout=30/timeout=60/' config.ini && git commit -q -am "Fix timeout too short for slow networks"
echo "format=pdf" >> reports.ini && git commit -q -am "Export reports as PDF"
git log --oneline
git switch -q main
git cherry-pick feature/reports~1
git log --oneline
ls
cat config.inia760df4 Export reports as PDF
a4662dc Fix timeout too short for slow networks
4262041 Add weekly reports
459b404 Initial config
[main 596e8f7] Fix timeout too short for slow networks
Date: Thu Jan 15 10:00:00 2026 +0000
1 file changed, 1 insertion(+), 1 deletion(-)
596e8f7 Fix timeout too short for slow networks
459b404 Initial config
config.ini
version=1.0
timeout=60
Only the timeout fix came over; the report commits stayed on their branch. The copy has a different hash from the original because it has a different parent.
Recording where it came from
-x appends "(cherry picked from commit …)" to the message, which helps people find the original later. Several commits can be picked at once, or a range A..B:
mkdir app && cd app && git init -q
echo "a" > log.txt && git add . && git commit -q -m "Start"
git switch -q -c dev
for fix in "Fix crash on empty input" "Fix typo in help text" "Fix memory leak"; do
echo "$fix" >> log.txt; git commit -q -am "$fix"
done
git switch -q main
git cherry-pick -x dev~2 2>&1 | head -1
git log -1 --format=%B[main 4d4dfbf] Fix crash on empty input
Fix crash on empty input
(cherry picked from commit 7ab50dfd533906560aabdcff2502c5788fba4f5a)
When a cherry-pick conflicts
If the commit depends on changes that aren't on your branch, you get a normal conflict. Fix the file, git add it, then git cherry-pick --continue (or --abort to give up):
mkdir app && cd app && git init -q
echo "greeting=Hello" > ui.ini && git add . && git commit -q -m "Start"
git switch -q -c redesign
echo "greeting=Hi" > ui.ini && git commit -q -am "Shorter greeting"
echo "greeting=Hi there" > ui.ini && git commit -q -am "Friendlier greeting"
git switch -q main
git cherry-pick redesign
git status -s
cat ui.ini
git cherry-pick --abort
git log --onelineAuto-merging ui.ini
CONFLICT (content): Merge conflict in ui.ini
error: could not apply 178c769... Friendlier greeting
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
UU ui.ini
<<<<<<< HEAD
greeting=Hello
=======
greeting=Hi there
>>>>>>> 178c769 (Friendlier greeting)
b695413 Start
Patches: commits as files
Before GitHub, patches sent by email were how projects like the Linux kernel accepted contributions, and they still are. git format-patch turns commits into .patch files; git am ("apply mailbox") turns them back into commits, keeping author and message:
mkdir upstream && cd upstream && git init -q
printf 'def area(r):\n return 3.14 * r * r\n' > circle.py && git add . && git commit -q -m "Add area"
cd ~ && git clone -q upstream contributor && cd contributor
sed -i 's/3.14/3.14159/' circle.py
git -c user.name="Grace Hopper" -c user.email=grace@example.com commit -q -am "Use a more precise value of pi"
git format-patch -1 -o ~/outbox
cat ~/outbox/*.patch/home/learner/outbox/0001-Use-a-more-precise-value-of-pi.patch
From 4d247493161eb7de49e0ca1b90ca5ea80bd7c41c Mon Sep 17 00:00:00 2001
From: Grace Hopper <grace@example.com>
Date: Thu, 15 Jan 2026 10:00:00 +0000
Subject: [PATCH] Use a more precise value of pi
---
circle.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/circle.py b/circle.py
index 931865c..55e518d 100644
--- a/circle.py
+++ b/circle.py
@@ -1,2 +1,2 @@
def area(r):
- return 3.14 * r * r
+ return 3.14159 * r * r
--
2.54.0
The maintainer applies it:
mkdir upstream && cd upstream && git init -q
printf 'def area(r):\n return 3.14 * r * r\n' > circle.py && git add . && git commit -q -m "Add area"
cd ~ && git clone -q upstream contributor && cd contributor
sed -i 's/3.14/3.14159/' circle.py
git -c user.name="Grace Hopper" -c user.email=grace@example.com commit -q -am "Use a more precise value of pi"
git format-patch -q -1 -o ~/outbox
cd ~/upstream
git am ~/outbox/0001-Use-a-more-precise-value-of-pi.patch
git log --format='%h %an: %s'Applying: Use a more precise value of pi
29167aa Grace Hopper: Use a more precise value of pi
c16a4b5 Ada Lovelace: Add area
Plain diffs: git diff + git apply
A plain git diff output is also a patch, just without the commit information. git apply applies it to the working folder without committing; --check tests it first:
mkdir site && cd site && git init -q
printf 'color: red\nsize: 12\n' > theme.txt && git add . && git commit -q -m "Theme"
sed -i 's/red/blue/' theme.txt
git diff > ~/blue.patch
git restore theme.txt
cat theme.txt
git apply --check ~/blue.patch && echo "patch applies cleanly"
git apply ~/blue.patch
cat theme.txt
git status -scolor: red
size: 12
patch applies cleanly
color: blue
size: 12
M theme.txt
| Tool | Carries | Applied with | Result |
|---|---|---|---|
git cherry-pick |
a commit in the same repository | a new commit | |
git format-patch |
commits, with author and message | git am |
new commits |
git diff > x.patch |
only the changes | git apply |
modified files, not committed |
Test yourself
What does git cherry-pick abc123 do?
- Moves commit abc123 to the current branch, deleting the original
- Creates a new commit on the current branch with the same change as abc123
- Merges the whole branch containing abc123
- Tags abc123
Why does a cherry-picked commit have a different hash than the original?
- Git adds a random number
- It has a different parent (and committer time), and the hash covers the parent
- Hashes are per-branch
- It doesn't, the hash is the same
Which command applies a patch produced by git format-patch as a commit with the original author?
- git apply
- git am
- git merge
- git patch