Git stores snapshots. Every other command is just navigating those snapshots.
Goal Command Pick one commit onto current branch git cherry-pick <sha>Pick with traceability git cherry-pick -x <sha>Pick multiple commits git cherry-pick <sha1> <sha2> <sha3>Pick a range (exclusive start) git cherry-pick <sha-from>..<sha-to>Pick a range (inclusive start) git cherry-pick <sha-from>^..<sha-to>Pick without auto-committing git cherry-pick --no-commit <sha>Resume after resolving conflict git cherry-pick --continueSkip current commit during range git cherry-pick --skipAbort entire cherry-pick git cherry-pick --abortPick a merge commit git cherry-pick -m 1 <sha> (rare)
Goal Command Save with descriptive message git stash push -m "WIP description"Save including untracked files git stash push -u -m "WIP description"List all stashes git stash listShow summary of a stash git stash show stash@{0}Show diff of a stash git stash show -p stash@{0}Apply most recent and remove git stash popApply specific stash and remove git stash pop stash@{2}Apply most recent, keep on stack git stash applyApply specific, keep on stack git stash apply stash@{2}Drop a specific stash git stash drop stash@{1}Drop ALL stashes (dangerous) git stash clearCreate branch from a stash git stash branch <branch> stash@{0}
Situation Tool One specific commit needed on another branch cherry-pick Backport a hotfix to a release branch cherry-pick All of branch X should land on branch Y merge Forward-port a release fix to main going forward cherry-pick (small fix) or merge (entire release) Salvage one good commit from an abandoned experiment cherry-pick Both branches will be merged later anyway wait for merge, don’t cherry-pick Commits depend on each other in subtle ways merge (all of them) or rebase the group
Situation Tool Need to swap branches for 15 minutes stash Need to pull latest, then continue stash Trying a quick experiment stash Going home for the day with half-done work WIP commit + push Handing off to a teammate WIP commit + push Multi-day project, not ready to commit cleanly WIP commit on a feature branch (squash later) Anything you’d be sad to lose if your laptop died WIP commit + push
echo " line 1 " > notes.txt
git commit -m " initial notes "
MAIN_SHA = $( git rev-parse HEAD )
git checkout -b feature/extra
echo " line 2 " >> notes.txt
git commit -m " add line 2 "
FEATURE_SHA = $( git rev-parse HEAD )
# Step 3: switch back and cherry-pick
git cherry-pick $FEATURE_SHA
# notes.txt now has line 1 and line 2
cat notes.txt # should show both lines
git log main --oneline # should show 2 commits, with the cherry-pick on top
# Step 6-8: reverse direction
echo " line 3 " >> notes.txt
git commit -m " add line 3 "
THIRD_SHA = $( git rev-parse HEAD )
git checkout feature/extra
git cherry-pick $THIRD_SHA
cat notes.txt # should show all three lines
The conflict in step 4-5 will look like:
>>>>>>> abc1234... (your cherry-picked commit)
Resolving by keeping port=8443 and running git cherry-pick --skip (rather than --continue) means the cherry-pick is abandoned for THIS commit. The cherry-pick exits without producing a new commit. If you were picking multiple commits and only this one conflicted, the others would still be picked normally.
# Setup: stash and then advance main
echo " stashed work " > old-file.txt
git stash push -u -m " old stash "
# Advance main with unrelated commits
echo " unrelated 1 " > other.txt
git commit -m " unrelated 1 "
# Now create the branch from the stash
git stash branch revive-old-work stash@{ 0 }
# You're now on `revive-old-work`, starting from where you stashed
# The stash is applied cleanly (no conflict with main's drift)
# The stash is dropped from the stack
git status # shows the stashed changes as modifications
Mistake Fix Cherry-picked the same commit twice git reset --hard HEAD~1 to drop the second copyCherry-pick stuck mid-conflict, want to give up git cherry-pick --abortStash pop gave conflict, don’t want to deal with it git checkout -- . to discard conflict state, stash still on stackStashed and immediately forgot the message git stash show -p stash@{0} to see the diffLost a stash with git stash drop Recovery via reflog is possible but tricky: git fsck --lost-found then inspect dangling commits Cherry-pick should have been a merge git reset --hard <pre-pick-sha> then git merge insteadCherry-pick a commit and then realize you need its whole branch Same fix: reset, then merge the source branch
Rebase, deeper. Interactive rebase (git rebase -i HEAD~5) lets you reorder, squash, fixup, edit, or drop commits in a range. Used for cleaning up history before opening a PR (collapsing “WIP” commits into one clean commit). The lesson covers when rebase is safe (your local branch, not yet pushed) and when it’s dangerous (published commits other people have pulled). Closes Phase 3.