git cherry-pick --edit <commit>

Apply a specific commit on top of the current branch while opening the commit message for editing.

This command lets you replay a specific commit onto your current branch and immediately tweak its commit message before finalizing it, which is useful when you want the code change but need to adjust the description for the new context.

This command uses cherry-pick to copy the changes from <commit> (for example, a hash like a1b2c3d4 or a reference like feature-branch~1) and applies them as a new commit on top of your current HEAD, and the --edit (or -e) flag tells Git to stop and open your default editor so you can modify the commit message before saving it. Under the hood, Git takes the diff introduced by <commit>, attempts to apply that diff to your current working tree, and if it succeeds without conflicts, it shows you the original commit message in your editor so you can keep it as-is, rewrite it, or expand it to better fit the target branch. If conflicts occur, you resolve them, run git add on the fixed files, then run git cherry-pick --continue to finish, or git cherry-pick --abort to cancel. Other related flags include --no-edit, which skips opening the editor and uses the original message as-is, and -x, which appends a cherry picked from line to the message to document where the change came from.

You can extend this pattern to multiple commits and slightly different workflows: using git cherry-pick <commit1> <commit2> applies several commits in order; combining --edit with multiple hashes (like git cherry-pick --edit a1b2c3d4 e5f6g7h8) opens the editor for each picked commit; while git cherry-pick A^..B applies a contiguous range from commit A's parent through B. If you want to interactively reshape a sequence of commits on the same branch instead of pulling from another branch, git rebase -i <base> can be a better fit, and if you want to experiment before making history changes permanent, using git branch temp-refactor followed by selective cherry-pick operations into another branch can give you a safer workflow.

Examples:

  • git cherry-pick --edit a1b2c3d4
  • git cherry-pick --edit feature-branch~1
  • git cherry-pick --edit 3f7c9e2
Manual page
git cherry-pick
Related commands

Welcome to GitExamples!

Sign in to gitexamples