Git Cherry Pick
Sometimes you make a commit to a branch that you meant to put on another one and you want to quickly fix it by popping that commit over to the correct branch.
Example situation
Current branch is great-new-stuff
, you have been working in this branch for awhile and then you get an urgent request to fix something on the main
branch and luckily for you the last commit you made on great-new-stuff
has this commit.
In this situation you want to do the following
- Read
git log
- Select the last entry
- Extract line with the
commit SHA
- Parse out the
SHA
- Echo back the correct command prefaced with
git cherry-pick
This page details the situation with illustrations
Here are two solutions that do this: PowerShell and Bash
PowerShell
"git cherry-pick $(((git log | Select-Object -First 1 $_.Line) -replace 'commit', '').Trim())" | clip
Bash
echo "git cherry-pick $(git log | head -n 1 | sed -e "s/commit//g" | xargs)" | xclip -selection c
Now check out the main
branch and paste the appropriate terminal command to pull the commit in.