72

I find myself doing this a lot when cherry-picking a commit from another branch.

$ git log -1 another_branch
commit <commit_sha>
// copy <commit_sha>
$ git cherry-pick <commit_sha>

Can I do all of this in one command, if so, what is it?

FoxyGio
  • 765
  • 1
  • 5
  • 5

1 Answers1

136

Just go with:

$ git cherry-pick another_branch

This will cherry-pick the last commit from another_branch.

Branches in git are just references to the last commit in that branch, you can use them instead of commit SHAs in your commands.

pbetkier
  • 1,657
  • 1
  • 13
  • 10
  • 2
    Doesn't work `git cherry-pick "dependabot/npm_and_yarn/web-app/graphql-codegen/cli-1.13.0" fatal: bad revision 'dependabot/npm_and_yarn/web-app/graphql-codegen/cli-1.13.0'`. Yes, that's a branch name. Git 2.25.1 – gadelat Mar 11 '20 at 08:45
  • @gadelat I have the same problem – Francesco Dondi Oct 19 '20 at 07:15
  • After switching to the new branch for applying the cherry-pick we can use `-` (hyphen) as alias for the hold branch, i.e.: `git cherry-pick -` – erny Jul 07 '23 at 08:55