How to cherry-pick multiple commits in git

You can cherry-pick multiple commits by issuing “git cherry-pick” command with initial and final commit numbers that you want to include.

The initial arrangement is as shown. There is a branch main with four commits. There is a branch maincopy that has six commits. We aim to cherry-pick the latest two commits of maincopy and apply them to main.

~/repoone$ git branch
* main
  maincopy
  maintemp
  source
  sourcecopy
  target

~/repoone$ git log --oneline
908d6ac (HEAD -> main, origin/main, origin/HEAD) Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit

~/repoone$ git log maincopy --oneline
59968f9 (maincopy) Second local commit of maincopy
10d3907 First local commit of maincopy
908d6ac (HEAD -> main, origin/main, origin/HEAD) Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit

First, checkout to main.

~/repoone$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

Issue “git cherry-pick” on commit numbers starting from 10d3907 to 59968f9. “^” implies that starting commit is also included. “..” implies a series of commits between these two commits including 59968f9. You can see using “git log” that two commits have been applied into main from maincopy.

~/repoone$ git cherry-pick 10d3907^..59968f9
[main a2a6c97] First local commit of maincopy
 Author: paperspace <Code Version Master>
 Date: Sat Sep 10 06:07:11 2022 +0000
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 localfileone
[main d087aaf] Second local commit of maincopy
 Author: paperspace <Code Version Master>
 Date: Sat Sep 10 06:07:26 2022 +0000
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 localfiletwo

~/repoone$ git log --oneline
d087aaf (HEAD -> main) Second local commit of maincopy
a2a6c97 First local commit of maincopy
908d6ac (origin/main, origin/HEAD) Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit

Do “git reset –hard” to reset to the original state of four commits in main.

~/repoone$ git reset --hard 908d6ac
HEAD is now at 908d6ac Third commit in main

This time, apply cherry-pick without the “^” symbol. You can see it would include all commits after 10d3907 till 59968f9. Only one commit is applied in this case.

~/repoone$ git cherry-pick 10d3907..59968f9
[main dbad508] Second local commit of maincopy
 Author: paperspace <Code Version Master>
 Date: Sat Sep 10 06:07:26 2022 +0000
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 localfiletwo

~/repoone$ git log --oneline
dbad508 (HEAD -> main) Second local commit of maincopy
908d6ac (origin/main, origin/HEAD) Third commit in main
42296d0 Second commit in main
453424e This is first commit
2673111 Initial commit