Skip to content
Visual explainer 3h to learn 5 steps

Rebase, cherry-pick, reset & reflog

Rewrite local history and recover 'lost' commits.

By Invicly Tech, , in Git, GitHub & GitHub Actions. About 11 minutes to read.

Topic 3 of 18

In short

Rebase, cherry-pick and reset rewrite or copy commits to tidy local history. The reflog records where HEAD has been, so almost any 'lost' commit can be recovered.

Explained simply

Before you share your work, you can tidy it. Maybe you made five commits called "wip" and "fix typo". Git lets you combine them, reword them, reorder them or drop some, so reviewers see a clean story. This is called rewriting history, and it is safe as long as nobody else has your commits yet.

Rebase moves your branch so it starts from a newer point, usually the latest main. Git takes each of your commits, one by one, and replays it on top. The result looks as if you started your work today. Cherry-pick does the same thing for a single commit: it copies one change onto the branch you are on.

Reset moves your branch pointer backwards (or anywhere). Depending on the mode, your changes stay staged, stay in your files, or are thrown away. It is the main tool for "undo my last commit".

The safety net is the reflog. Every time HEAD moves, Git writes a line in a private log. Even if a rebase or reset seems to destroy commits, they are still in the database and the reflog tells you where. Only work you never committed is truly at risk.

Press Play on a diagram to walk through it one step at a time.

Comparegit reset modes
--soft
  • Moves the branch pointer
  • Changes stay staged
  • Good for recommitting
--mixed (default)
  • Moves the branch pointer
  • Changes kept but unstaged
  • Good for re-splitting commits
--hard
  • Moves the branch pointer
  • Discards working changes
  • Destructive; check first
FlowRecover a lost commit
  1. Step 1
    Run git reflog

    Lists every position HEAD has held, newest first.

  2. Step 2
    Find the entry

    Look for the commit message before the mistake.

  3. Step 3
    Create a branch

    git branch rescue HEAD@{3} keeps it safe.

  4. Step 4
    Merge or reset

    Bring the rescued commits back where needed.

CompareWhich undo tool to use
git revert
  • Adds an undo commit
  • History unchanged
  • Safe on shared branches
  • Works for any old commit
git reset
  • Moves the branch back
  • Removes commits from the branch
  • Only for unshared work
  • Mode decides what happens to files
git rebase -i
  • Edits a range of commits
  • Squash, reword, drop
  • Creates new hashes
  • Tidy before opening a PR

In depth

What rebase really does

git rebase main on a feature branch runs a precise recipe. Git finds the merge base, lists the commits on your branch after it, saves the old tip as ORIG_HEAD, moves your branch to main's tip, and then applies each saved commit in order, like a series of cherry-picks. Each applied commit gets a new parent, so it gets a new hash. The old commits still exist, unreferenced.

If any step conflicts, the rebase pauses on that one commit. You fix it, git add, and git rebase --continue. git rebase --abort puts everything back as it was. Right after a finished rebase, git reset --hard ORIG_HEAD undoes it in one step.

Flowgit rebase main, step by step
  1. 1
    Find merge base

    Where the branch left main

  2. 2
    Save your commits

    Old tip kept as ORIG_HEAD

  3. 3
    Jump to main's tip

    Branch now starts from new main

  4. 4
    Replay commits

    One by one, each gets a new hash

  5. 5
    Move branch

    Points at the last replayed commit

Interactive rebase and autosquash

git rebase -i HEAD~4 opens a to-do list of your last four commits, oldest first. Change the word in front of each line: pick keeps it, reword edits the message, edit stops so you can change the content, squash melds it into the previous commit and combines messages, fixup does the same but discards this message, and drop removes it. You can also reorder lines.

A faster habit is to mark fixes as you go. git commit --fixup=HASH creates a commit titled fixup! plus the target's title. Later, git rebase -i --autosquash main moves each fixup under its target and marks it fixup automatically. Set rebase.autoSquash true to make that the default.

  • pick, reword, edit, squash, fixup, drop
  • exec npm test runs a command after a commit is replayed
  • --rebase-merges keeps merge commits instead of flattening them

Cherry-pick in practice

Cherry-pick takes the change a commit introduced (its diff against its parent) and applies it as a new commit on your current branch. The classic use is a hotfix: fix the bug on main, then copy that one commit to release/1.4. Add -x so the new message records "cherry picked from commit ...", which helps people trace it later.

You can pick several commits at once: git cherry-pick A..B copies everything after A up to and including B. Picking a merge commit needs -m 1 to say which parent is the mainline. Be careful about copying the same change to many branches: later merges can conflict, and the history shows the same change twice under different hashes.

Reflog, expiry and what cannot be saved

Each repository keeps a reflog for HEAD and for every branch. Entries look like HEAD@{3}, meaning where HEAD was three moves ago, and you can also write main@{yesterday} or HEAD@{2.hours.ago}. By default, entries for commits still reachable are kept for 90 days, and entries for unreachable commits for 30 days, before git gc can clean them up.

The reflog is local. It is not pushed or cloned, so a fresh clone has no record of your old moves. It also only tracks commits. Changes you never committed (wiped by reset --hard or git checkout -- file) are not in it. A dropped stash is a commit too, so git fsck --unreachable can still find it for a while.

Rewriting shared branches safely

Rewriting a branch that others have pulled forces them to untangle duplicates, so the rule is: rewrite only your own, unshared commits. On shared branches, use git revert, which adds a new commit that undoes an old one without changing history.

When you must push a rewritten branch (for example after rebasing your own PR), use git push --force-with-lease. It refuses to overwrite the remote if it has commits you have not seen, protecting a teammate's push. Plain --force overwrites blindly. Branch protection on main should block force pushes entirely.

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Rebase a branch onto main~35m
  2. 2
    Squash commits with interactive rebase~35m
  3. 3
    Cherry-pick a fix across branches~35m
  4. 4
    Compare soft, mixed and hard reset~35m
  5. 5
    Recover lost commits with reflog~35m

Key points

  • Rebase replays your commits on a new base, creating new commits with new hashes.
  • Interactive rebase lets you squash, reword, reorder or drop local commits.
  • Cherry-pick copies one commit onto another branch, such as a hotfix to release.
  • Never rewrite commits others already pulled; rewrite only local or private branches.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
git fetch origin
git rebase origin/main
git rebase -i HEAD~3          # mark commits as squash or fixup
git cherry-pick 4f2a9c1       # copy a fix onto this branch
git reset --soft HEAD~1       # undo last commit, keep changes staged
git reflog
git branch rescue HEAD@{2}
git push --force-with-lease

Tidy, copy, undo and recover.

Turn messy commits into a clean PR

Your branch has: Add search API, wip, fix test, Add search UI. Step 1: start an interactive rebase onto main. Step 2: change wip and fix test from pick to fixup, so they melt into Add search API. Step 3: save and close; Git replays the commits. Step 4: check the log, run the tests, and push with force-with-lease because the hashes changed.

text
$ git rebase -i origin/main

pick 1a2b3c4 Add search API
fixup 5d6e7f8 wip
fixup 9a8b7c6 fix test
pick 3c4d5e6 Add search UI

$ git log --oneline origin/main..HEAD
e1f2a3b Add search UI
7c8d9e0 Add search API
$ git push --force-with-lease

Undo a bad reset --hard

You ran git reset --hard HEAD~3 on the wrong branch and three commits vanished from git log. Step 1: run git reflog to see where HEAD was before the reset. Step 2: the line just before reset: moving to HEAD~3 shows the old tip. Step 3: reset back to it. The commits come back exactly as they were, because they never left the object database.

bash
git reflog -4
# a9f3c21 HEAD@{0}: reset: moving to HEAD~3
# 7e4b0d2 HEAD@{1}: commit: Add export button
# ...
git reset --hard HEAD@{1}
git log --oneline -3   # the commits are back

Quick reference

  • git rebase -i --autosquash origin/main folds fixup! commits into their targets
  • git commit --fixup=HASH marks a commit to be squashed into an earlier one
  • git cherry-pick -x HASH copies a commit and records where it came from
  • git reset --hard ORIG_HEAD undoes the last rebase, merge or reset
  • git commit --amend replaces the last commit; never amend pushed shared commits
  • git revert HASH undoes a shared commit safely with a new commit
  • git push --force-with-lease only overwrites if the remote is what you last saw
  • git rebase --onto main old-base feature moves a branch onto a different base

Common mistakes

  • Using git push --force on shared branches; prefer --force-with-lease and only on your own.
  • Running reset --hard with uncommitted work, which reflog cannot recover.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1In an interactive rebase, what is the difference between squash and fixup?

Question 2

2A bug fix on main must also go to release/2.1. What is the usual tool?

Question 3

3What does git push --force-with-lease protect against?

Question 4

4Which of these can the reflog NOT help you recover?

Question 5

5You undo a commit that is already on the shared main branch. What should you use?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Make a commit with a typo in its message, then fix the message without creating a second commit.

  2. Exercise 2medium

    Make three commits, then use reset to turn them into a single commit with a new message, without interactive rebase.

  3. Exercise 3hard

    Your feature branch was cut from release/1.0 by mistake but should be based on main. Move only your 2 commits onto main.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is rebase dangerous?
Not for commits only you have. It creates new commits and the old ones stay in the reflog, so you can undo it. It becomes painful only when you rebase commits other people already pulled.
What is the difference between git reset and git restore?
git reset moves a branch pointer (and can reset staging). git restore changes files in your working folder or index without touching any branch.
How long does Git keep commits I lost?
By default, reflog entries for unreachable commits expire after 30 days, and reachable ones after 90, before garbage collection may delete them.
Should I rebase or merge main into my feature branch?
Rebase gives a cleaner history and is fine for your own branch. Merging main in is safer when several people share the branch, because nothing is rewritten.

Go deeper

ShareXLinkedInWhatsApp