日本語

How to Cherry-Pick Specific Commits with git cherry-pick

​​Git​command line

To bring only a bug fix from the development branch (main) into a release branch, use git cherry-pick. Merging the whole branch would also bring in changes you are not ready to release. With cherry-pick, you can record the changes from a chosen commit as a new commit on your current branch. This article shows the basic single-commit pick, using -x to record the original commit, picking several separate commits, and resolving conflicts, with real output from before and after each step.

Note

The test environment is Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>), and fixed the commit dates with the environment variables GIT_AUTHOR_DATE and GIT_COMMITTER_DATE so the output is the same every time. All tests were run only in a local repository, without pushing. I did not test remote operations or Windows and macOS.

The branch before picking

main has five commits. release/1.0 branches off from the first commit (8856065). This article picks only "Fix wrong opening hours" (5447d2f) from main into release/1.0. The README and price table changes are not wanted yet.

console
$ git switch release/1.0
Switched to branch 'release/1.0'

$ git log --oneline --graph --all
* ab2fca7 定休日を追加
* 22bf37c READMEに準備中の案内を追加
* 5447d2f 営業時間の誤りを直す
* f4856ab 料金表に手数料を追加
* 0e49641 READMEを追加
* 8856065 最初のコミット

$ git log --oneline
8856065 最初のコミット

$ cat news.txt
営業時間: 10時から18時

Before picking, use git show to check what the commit changes. With --stat, you see only the changed files and line counts.

console
$ git show --stat --format="%h %s" main~2
5447d2f 営業時間の誤りを直す

 news.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

main~2 refers to the commit two steps before the latest commit on main (here, 5447d2f).

Picking one commit

While on the branch you want to bring changes into (release/1.0), pass the hash of the commit to pick to git cherry-pick.

console
$ git cherry-pick 5447d2f
[release/1.0 29511f8] 営業時間の誤りを直す
 Date: Fri Nov 13 10:30:00 2026 +0900
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git log --oneline
29511f8 営業時間の誤りを直す
8856065 最初のコミット

$ cat news.txt
営業時間: 10時から19時

A new commit, "Fix wrong opening hours", was added to release/1.0, and news.txt is fixed. The README and price table changes were not brought in.

However, the new commit has the hash 29511f8, not the 5447d2f from main. This is because cherry-pick applies the changes made by the specified commit to the current branch and records them as a new commit (official git cherry-pick reference). In the graph, release/1.0 has moved one step forward as a separate line and does not merge back into the history of main.

console
$ git log --oneline --graph --all
* 29511f8 営業時間の誤りを直す
| * ab2fca7 定休日を追加
| * 22bf37c READMEに準備中の案内を追加
| * 5447d2f 営業時間の誤りを直す
| * f4856ab 料金表に手数料を追加
| * 0e49641 READMEを追加
|/  
* 8856065 最初のコミット
Before cherry-pick, release/1.0 points to 8856065. After, 29511f8 is added on top of 8856065 and release/1.0 points to it. 29511f8 is a different commit that has the same changes as 5447d2f
Commit connections before and after cherry-pick

Next, look at the dates of the new commit. The author date (AuthorDate) is still 10:30, the same as the original commit, while the commit date (CommitDate) is 11:00, when it was picked. The line Date: Fri Nov 13 10:30:00 2026 in the output above shows that the original author date was kept.

console
$ git show --format="commit %H%nAuthor: %an <%ae>%nAuthorDate: %ad%nCommit: %cn <%ce>%nCommitDate: %cd%n%n    %s" --no-patch HEAD
commit 29511f8750838c1c77e4c7ddc107ddfc76d742b9
Author: Neko <neko@example.com>
AuthorDate: Fri Nov 13 10:30:00 2026 +0900
Commit: Neko <neko@example.com>
CommitDate: Fri Nov 13 11:00:00 2026 +0900

    営業時間の誤りを直す

$ git show --format="commit %H%nAuthorDate: %ad%nCommitDate: %cd" --no-patch main~2
commit 5447d2f25ca3d434e2f66558922b31e2eed42642
AuthorDate: Fri Nov 13 10:30:00 2026 +0900
CommitDate: Fri Nov 13 10:30:00 2026 +0900

Using -x to record where a commit came from

Because the hash changes, looking at the history of release/1.0 later does not show which commit on main it came from. With -x, the full hash of the original commit is added to the end of the commit message.

I undid the previous pick with git reset --hard HEAD~1, then picked again with -x.

console
$ git reset -q --hard HEAD~1

$ git log --oneline
8856065 最初のコミット

$ git cherry-pick -x 5447d2f
[release/1.0 d59656f] 営業時間の誤りを直す
 Date: Fri Nov 13 10:30:00 2026 +0900
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git log -1 --format="%h %s%n%n%b"
d59656f 営業時間の誤りを直す

(cherry picked from commit 5447d2f25ca3d434e2f66558922b31e2eed42642)

The official reference's description of -x says this information is useful when picking between branches that are both published, such as moving a fix from a development branch to the maintenance branch of an older release. It also says not to add it when picking from a local branch that only you use, because the information has no meaning for the person receiving the commit.

Warning

git reset --hard also discards uncommitted changes to files tracked by Git. Here, the working tree had no changes when I ran it.

Picking the same commit again

I tried to pick the same 5447d2f again on release/1.0, which already has it from the -x pick.

console
$ git cherry-pick 5447d2f
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

Otherwise, please use 'git cherry-pick --skip'
On branch release/1.0
You are currently cherry-picking commit 5447d2f.
  (all conflicts fixed: run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

nothing to commit, working tree clean

The change is already present, so applying it produces no difference and the commit would be empty. By default, cherry-pick stops at a commit that would become empty (official reference on –keep-redundant-commits). As the output says, git cherry-pick --skip skips this commit and finishes.

console
$ git cherry-pick --skip

$ git status -sb
## release/1.0

$ git log --oneline -1
d59656f 営業時間の誤りを直す

No commit was added, and release/1.0 still ends at d59656f.

Picking two separate commits together

git cherry-pick accepts several commits in one command. Here, the two commits "Add price table fee" (f4856ab) and "Add regular closed day" (ab2fca7) from main are picked. 22bf37c in between is not picked.

console
$ git log --oneline main
ab2fca7 定休日を追加
22bf37c READMEに準備中の案内を追加
5447d2f 営業時間の誤りを直す
f4856ab 料金表に手数料を追加
0e49641 READMEを追加
8856065 最初のコミット

$ git cherry-pick f4856ab ab2fca7
[release/1.0 d1b4e04] 料金表に手数料を追加
 Date: Fri Nov 13 10:20:00 2026 +0900
 1 file changed, 1 insertion(+)
[release/1.0 e61db26] 定休日を追加
 Date: Fri Nov 13 10:50:00 2026 +0900
 1 file changed, 1 insertion(+)

$ git log --oneline
e61db26 定休日を追加
d1b4e04 料金表に手数料を追加
d59656f 営業時間の誤りを直す
8856065 最初のコミット

$ cat price.txt news.txt
送料: 500円
手数料: 200円
営業時間: 10時から19時
定休日: 水曜日

Two commits were created, one for each commit in the order given.

To pick a run of consecutive commits, you can use the range notation A..B. In this notation, the commit written on the left, A, is not included. I checked which commits git log includes for each range. main~4..main~2 did not include main~4 (0e49641). Adding ^ as in main~4^..main~2 included it. ^ refers to the parent of that commit, that is, the commit one step before it.

console
$ git log --oneline main~4..main~2
5447d2f 営業時間の誤りを直す
f4856ab 料金表に手数料を追加

$ git log --oneline main~4^..main~2
5447d2f 営業時間の誤りを直す
f4856ab 料金表に手数料を追加
0e49641 READMEを追加

When there is a conflict: fix it and –continue, or –abort to cancel

A conflict happens when the same lines have already been changed to something different on the target branch. release/0.9 branches off from the first commit, and it has changed the opening hours to 9:00–17:00. Here, 5447d2f (the fix that changes the hours to 10:00–19:00) is picked into it.

console
$ git log --oneline
67ff7a0 営業時間を9時からに変更
8856065 最初のコミット

$ git cherry-pick 5447d2f
Auto-merging news.txt
CONFLICT (content): Merge conflict in news.txt
error: could not apply 5447d2f... 営業時間の誤りを直す
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".

$ git status -sb
## release/0.9
UU news.txt

$ cat news.txt
<<<<<<< HEAD
営業時間: 9時から17時
=======
営業時間: 10時から19時
>>>>>>> 5447d2f (営業時間の誤りを直す)

The part from <<<<<<< HEAD to ======= is the content on the current branch, and the part below it is the content of the commit being picked. No commit was created, and the branch stays where it was.

To cancel the pick, run git cherry-pick --abort. This returns to the state from before cherry-pick started.

console
$ git cherry-pick --abort

$ git status -sb
## release/0.9

$ cat news.txt
営業時間: 9時から17時

$ git log --oneline -1
67ff7a0 営業時間を9時からに変更

To resolve and pick, start cherry-pick again to produce the conflict, then edit the file into the form you want. Here I wrote 9:00–19:00. Then run git add on the fixed file and git cherry-pick --continue.

console
$ printf "営業時間: 9時から19時\n" > news.txt

$ git add news.txt

$ git cherry-pick --continue
[release/0.9 6ee537a] 営業時間の誤りを直す
 Date: Fri Nov 13 10:30:00 2026 +0900
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git log --oneline
6ee537a 営業時間の誤りを直す
67ff7a0 営業時間を9時からに変更
8856065 最初のコミット

$ cat news.txt
営業時間: 9時から19時

In this test, I committed at --continue without opening an editor, keeping the same message as the original commit. When a conflict happens while picking several commits, --continue moves on to the remaining commits, and --skip skips only the current commit and moves on to the rest (official reference on --continue, --skip, and --abort).

​Posted by 86