Rebase, stash & cherry-pick
Earlier chapters mentioned rebase, stash and reflog in a line each. This one goes deeper: how rebase rewrites history, cleaning up commits before a pull request, handling conflicts mid-rebase, juggling several stashes, copying a single commit between branches, and using the reflog to get out of almost any mess.
Rebase vs merge
You branched feature off main and made commits A, B, C; meanwhile main got F and G. git rebase main replays your commits one by one on top of G:
before:
A---B---C feature
/
D---E---F---G main
after git rebase main:
A'--B'--C' feature
/
D---E---F---G main
A', B', C' contain the same changes but are new commits with new hashes, because their parent changed. History becomes a straight line, and merging into main is a fast-forward. Merge never rewrites anything but adds merge commits; rebase gives clean, linear history but rewrites commits. Many teams rebase their own branch to keep it current, then merge (or squash-merge) the pull request.
$ git switch feature/login
$ git fetch origin
$ git rebase origin/main
Successfully rebased and updated refs/heads/feature/login.
$ git push --force-with-lease # the branch was pushed before, and its history changed
A normal push is now rejected because the remote still has the old commits. --force-with-lease overwrites only if nobody else pushed since your last fetch; plain --force would silently delete their work. git pull --rebase (or git config --global pull.rebase true) makes pulls rebase instead of merge.
main or a shared branch. Their copies keep the old commits, and the next pull brings duplicates and confusing conflicts. Rebasing your own PR branch is fine as long as nobody else commits to it.Interactive rebase: clean up before review
$ git rebase -i HEAD~4 # the last 4 commits
# or: git rebase -i main # everything on this branch since it left main
Git opens a to-do list, oldest first. Change the command word on each line, then save and close:
pick 1a2b3c4 Add login form
pick 5d6e7f8 fix typo
pick 9a0b1c2 WIP
pick 3d4e5f6 Add password validaton
# edited to:
pick 1a2b3c4 Add login form
fixup 5d6e7f8 fix typo
fixup 9a0b1c2 WIP
reword 3d4e5f6 Add password validaton
pick: keep as is.reword: keep, but edit the message (fixing "validaton").squash: melt into the commit above and edit the combined message.fixup: same, but discard this commit's message.edit: stop here to change the content (git commit --amend, thengit rebase --continue).dropor deleting the line: remove the commit.
Four messy commits become two clean ones; reordering lines reorders commits. Shortcut: commit a fix with git commit --fixup=1a2b3c4, and git rebase -i --autosquash main later puts it in place, already marked fixup.
Conflicts during a rebase
$ git rebase main
CONFLICT (content): Merge conflict in app.py
error: could not apply 6f700cc... Add login form
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
- Fix the conflict markers in the file, exactly as for a merge conflict.
git add app.py. Don't rungit commit: rebase commits for you.git rebase --continue. The next commit may conflict too; repeat until "Successfully rebased".
git rebase --abort puts everything back as before you started, and is always safe. --skip drops the conflicting commit. git status shows where you are.
git checkout --ours/--theirs or VS Code's "Accept Current / Incoming".Stash in depth
The stash is a stack: the newest entry is stash@{0}. Name your entries:
$ git stash push -m "half-done navbar" # tracked changes, staged and unstaged
$ git stash push -u -m "with new files" # -u also takes untracked files
$ git stash push -m "just css" -- style.css # only some paths
$ git stash list
stash@{0}: On feature/nav: just css
stash@{1}: On feature/nav: with new files
stash@{2}: On feature/nav: half-done navbar
$ git stash show -p stash@{2} # the diff inside an entry
$ git stash apply stash@{2} # apply, keep it in the list
$ git stash pop # apply stash@{0} and remove it
$ git stash drop stash@{1} # delete one entry
$ git stash branch fix-nav stash@{2} # new branch from where you stashed, changes applied
- If
pophits a conflict, the entry is not dropped: resolve, thengit stash dropyourself. - Without
-u, new files aren't stashed and follow you to the other branch. - Stashes are local and never pushed. For anything long-lived, commit to a WIP branch instead.
Cherry-pick: copy one commit
Classic case: a bug fix landed on main, and release/1.4 needs it without everything else from main.
$ git switch release/1.4
$ git cherry-pick -x 9f1c2ab # -x adds "(cherry picked from commit 9f1c2ab...)" to the message
$ git cherry-pick 4e5d6c7 9f1c2ab # several, in that order
$ git cherry-pick A..B # commits after A, up to and including B
$ git cherry-pick A^..B # the same range including A
The copy gets a new hash. Conflicts work like in a rebase: fix, git add, git cherry-pick --continue (or --abort). If you're cherry-picking many commits, you probably want a merge or rebase instead.
The reflog: your undo history
Every time HEAD moves (commit, checkout, reset, rebase), Git logs it. Commits no branch points to are still reachable there. After an accidental git reset --hard HEAD~2:
$ git reflog
6ee1bf6 (HEAD -> feature/login) HEAD@{0}: reset: moving to HEAD~2
8634d90 HEAD@{1}: commit: Add password validation
6f700cc HEAD@{2}: commit: Add login form
$ git reset --hard 8634d90 # jump back to before the reset
$ git branch rescue 8634d90 # or: keep things as they are and save the commit on a branch
- Undo a rebase you just finished:
git reset --hard ORIG_HEAD(Git saves the old tip there before rebases, merges and resets). - Recover a deleted branch: find its last commit in the reflog, then
git branch name <hash>. - The reflog is local and entries expire (by default 90 days, or 30 for commits no longer on any branch). It can't bring back changes that were never committed or stashed.
Common problems
- "Updates were rejected" after rebasing: expected.
git push --force-with-leaseon your own branch, never on main. - Someone rebased a shared branch: with no local work on it,
git fetchthengit reset --hard origin/<branch>. - Stuck "in the middle of a rebase":
git statusexplains;git rebase --abortgets you out. - Squashed the wrong commits:
git reset --hard ORIG_HEADand try again.