Time Travel & History Surgery: `git reset` (Soft, Mixed, Hard) & `git cherry-pick`
Every engineer makes mistakes: committing half-finished code, including accidental API keys, writing typo-ridden commit messages, or wanting a single bug-fix commit from an experimental branch without merging weeks of unfinished work. Git provides surgical time-travel primitives to handle these exact scenarios. In this chapter, you will master `git reset` across its three distinct modes (`--soft`, `--mixed`, `--hard`), understand why you should never reset public history (and use `git revert` instead), and perform surgical commit transplants using `git cherry-pick`.
- 01Deconstruct how Git's Three Trees (HEAD, Index/Staging, and Working Directory) behave during undo operations
- 02Master the three operational modes of git reset: --soft (preserves staging), --mixed (un-stages changes), and --hard (wipes changes)
- 03Safely recover from accidental data loss using Git's internal flight recorder: git reflog
- 04Internalize the Golden Rule of Git: why git reset is strictly for local private history, and git revert is used for pushed public history
- 05Surgically transplant individual commits from one branch to another using git cherry-pick
- 06Resolve and finalize cherry-pick merge conflicts using git cherry-pick --continue and git cherry-pick --abort
4.1 The Anatomy of Undoing: Git's Three Trees Revisited
4.2 `git reset` Demystified: The Three Modes (--soft, --mixed, --hard)
Undo a commit softly to amend code, and use git reflog to recover from a hard reset.
# Scenario A: Soft reset to reword or add files to the last commit
git commit -m "wip: bad commit message with typos"
# Undo the commit, keeping all changes staged in the Index
git reset --soft HEAD~1
# Check status: notice changes are still staged (green)!
git status -s
# Re-commit with a clean, professional commit message
git commit -m "feat(heuristics): implement baseline risk score threshold"
# Scenario B: Hard reset to discard an experimental failure
# DANGER: permanently overwrites modified files in working directory!
git reset --hard HEAD~1
# Scenario C: Recovering an 'erased' commit using Git's flight recorder (reflog)
git reflog
# Output shows: 7a8b9c0 HEAD@{1}: commit: ...
# Restore the lost commit:
git reset --hard HEAD@{1}- 1.Run git reset --soft HEAD~1: the commit disappears from git log, but your files remain staged.
- 2.Run git status to verify files are staged and ready for a revised commit message.
- 3.If git reset --hard is ever run accidentally, immediately run git reflog to locate the lost commit hash.
- 4.Run git reset --hard <hash> to restore your repository to that exact point in time.
[main a1b2c3d] feat(heuristics): implement baseline risk score threshold 1 file changed, 3 insertions(+)
• Uncommitted files (untracked or unstaged modifications) overwritten by git reset --hard CANNOT be recovered by reflog, because Git never took a snapshot of them! Always commit or stash before running a hard reset.
Why it happens: Git never deletes commit objects immediately. Even after a hard reset, the commit objects remain in .git/objects for weeks until garbage collection runs, letting reflog rescue you.
4.3 Reversing Public Commits Safely: `git revert`
Create an inverse commit to safely undo a change on a shared branch.
# View recent commit hashes
git log --oneline -n 3
# Revert a specific buggy commit (creates a new inverse commit)
git revert a1b2c3d --no-edit
# Inspect history: notice a new 'Revert ...' commit exists
git log --oneline -n 2- 1.Find the hash of the commit you need to undo with git log --oneline.
- 2.Run git revert <hash>.
- 3.Git applies the inverse diff and creates a new commit snapshot.
- 4.Push the new revert commit to GitHub with standard git push.
[main e5f6a7b] Revert "feat: experimental payment endpoint" 1 file changed, 10 deletions(-)
Why it happens: Linear history is preserved, and your teammates will cleanly pull the reversal without any merge conflicts.
4.4 Surgical Commit Transplant: `git cherry-pick`
Extract a specific commit from a feature branch and transplant it onto main.
# Step 1: Inspect the feature branch to find the desired commit hash
git log --oneline feature/experimental-heuristics -n 3
# Step 2: Switch to the destination branch (main)
git switch main
# Step 3: Cherry-pick the specific bug-fix commit (e.g. c8d9e0f)
git cherry-pick c8d9e0f
# If a conflict occurs during cherry-pick:
# 1. Edit the conflicted file to resolve markers
# 2. Stage the resolved file: git add <file>
# 3. Complete cherry-pick: git cherry-pick --continue
# (Or abort safely: git cherry-pick --abort)- 1.Use git log to identify the SHA-1 hash of the specific commit you want.
- 2.Always switch to the destination branch before cherry-picking.
- 3.Run git cherry-pick <hash>.
- 4.Verify with git log --oneline: the commit now appears at the top of your destination branch.
[main 4a5b6c7] fix(security): sanitize email header control characters 1 file changed, 5 insertions(+) create mode 100644 security.py
• Cherry-pick conflicts: If the code in the cherry-picked commit depends on prior changes on the source branch that do not exist on main, Git halts with a conflict. Resolve the file, git add it, and run git cherry-pick --continue.
Why it happens: Cherry-pick applies a diff rather than merging branch pointers, making it ideal for hotfixes and extracting isolated utilities.
Practice exercise
Work through the exercise before starting the chapter assignment
Exercise 4: Practice Soft Reset & Cherry-Pick Workflow
intermediateOpen your terminal in a test Git repository and execute: (1) create a test commit, (2) undo it softly with git reset --soft HEAD~1, (3) verify changes remain staged with git status, and (4) cherry-pick a commit using its hash.
Chapter summary
Key concepts and syntax cards for review
- 01git reset moves the branch pointer and HEAD backward across your commit history
- 02--soft moves HEAD but preserves Staging Index and Working Tree (ideal for fixing commit messages)
- 03--mixed (default) moves HEAD and un-stages files, preserving Working Tree modifications
- 04--hard moves HEAD, un-stages files, and overwrites working files (destructive!)
- 05git reflog records every movement of HEAD, enabling recovery of commits lost during hard resets
- 06Never reset pushed public history; use git revert to create a safe inverse forward commit
- 07git cherry-pick surgically transplants an individual commit from one branch to another
git reset --soft HEAD~1Undoes the latest commit, keeping all changes staged in the Indexgit reset --mixed HEAD~1Undoes latest commit and un-stages changes into the working directory (default)git reset --hard HEAD~1Undoes latest commit and discards all uncommitted working directory changesgit reflogDisplays the chronological log of all HEAD pointer movements for disaster recoverygit revert <commit-hash>Creates a new commit that applies the exact inverse changes of a targeted commitgit cherry-pick <commit-hash>Applies the changes from a specific commit on another branch onto the current branchgit cherry-pick --continueResumes an interrupted cherry-pick operation after resolving merge conflictsgit cherry-pick --abortCancels an in-progress cherry-pick conflict and restores original branch stateKnowledge check
Verify your grasp of the chapter concepts
Which command undoes your latest commit while keeping all modified files safely staged in the Index?
When should you use 'git cherry-pick <commit>'?