Skip to main content
blog.philz.dev

Git Virtual Cherry-Pick

Thing I learned recently from Josh Bleecher Snyder: there are enough git plumbing commands now that you can produce a commit object that's the same as "checkout origin/main and cherry-pick one-commit" with just plumbing, without creating worktrees, using temporary indexes, and so on.

The magic incantation is (where c is the commit to cherry-pick):

git commit-tree \
  "$(git merge-tree --write-tree --no-messages --merge-base c^ origin/main c)" \
  -p origin/main \
  -F <(git log -1 --format=%B c)

That's a mouthful, but the core trick is specifying merge-base explicitly in there.

This is handy if you haven't yet internalized jj and want to push one commit in a stack.

For more git tricks, see also: