Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Git

    Undoing Things

    Different kinds of "undo" for different situations: rubbing out a pencil mark you haven't photographed yet; taking a

    Chapter 9 of 115 min readOften asked in interviews

    The Plain Picture#

    Different kinds of "undo" for different situations: rubbing out a pencil mark you haven't photographed yet; taking a photo out of the album before anyone saw it; or, if friends already have copies of the album, adding a new photo that says "the previous photo was a mistake — here's the correction".

    The Real Names — pick the right tool#

    SituationCommandWhat it does
    Throw away unstaged edits to a filegit restore quiz.pyresets the working file to the last commit. Edits are lost.
    Unstage a file (keep the edits)git restore --staged quiz.pytakes it out of the staging area
    Fix the last commit's message, or add a forgotten filegit commit --amendreplaces the last commit with a new one (new hash)
    Undo local commits, keep the changes stagedgit reset --soft HEAD~1moves the branch back one commit
    Undo local commits, keep the changes unstagedgit reset HEAD~1 (--mixed, the default)
    Undo local commits and throw the changes awaygit reset --hard HEAD~1⚠️ discards work
    Undo a commit that's already pushedgit revert <hash>creates a new commit that reverses it — safe for shared branches
    Park unfinished work to switch tasksgit stash / git stash popsaves changes aside and cleans the working directory / brings them back
    "I lost a commit!"git reflogshows everywhere HEAD has been — find the hash and recover it

    (HEAD~1 means "one commit before HEAD"; HEAD~3 means three before.)

    Under the Hood#

    reset vs revert — the key interview question#

      git reset --hard HEAD~1                      git revert B
      (rewrites history — local only!)             (adds history — safe anywhere)
    
      A───B───C  main   →   A───B  main            A───B───C  main   →   A───B───C───C'  main
              ▲                  ▲                                                   ▲
           removed          branch moved back                      C' = "undo the changes made in C"
    • reset moves the branch pointer backwards — commits after it vanish from the branch. Fine for commits only you have. On pushed commits it causes the same trouble as rebasing shared work (Ch 6).

    • revert leaves history alone and adds a new commit with the opposite changes. It's the right way to undo something on main — for example, a deploy that broke production.

    reflog — Git's safety net#

    Git almost never truly deletes a commit immediately. git reflog records every position HEAD has had, including commits "lost" through reset, rebase or a detached HEAD. From the practice repo:

    Output
    $ git reflog
    80b9b6d HEAD@{0}: commit (merge): Merge branch 'bonus'
    640a7d3 HEAD@{1}: commit: Double points
    7ef9500 HEAD@{2}: checkout: moving from bonus to main
    9ffbfe2 HEAD@{3}: commit: Add bonus points
    7ef9500 HEAD@{4}: checkout: moving from main to bonus

    If you ran git reset --hard and regretted it, find the commit you want in the reflog and bring it back: git reset --hard 640a7d3 (or git switch -c recovered 640a7d3). Unreachable commits are cleaned up eventually (after about 30–90 days by default), so recover sooner rather than later.

    What reflog can't save: changes that were never committed or stashed. git restore and git reset --hard on uncommitted work are truly destructive. Commit or stash first.

    Check yourself

    5 questions
    1

    You committed with a typo in the message and haven't pushed. How do you fix it?

    Show answer

    git commit --amend -m "correct message".

    2

    A bad commit is already on main and deployed. How do you undo it, and why not reset?

    Show answer

    git revert <hash> — it adds a new undoing commit, so shared history isn't rewritten; reset would break everyone who already has the commit.

    3

    What's the difference between reset --soft, --mixed and --hard?

    Show answer

    All move the branch back. --soft keeps the changes staged, --mixed keeps them unstaged, --hard throws them away.

    4

    You ran git reset --hard HEAD~2 by mistake. How do you get the commits back?

    Show answer

    git reflog, find the commit hash from before the reset, and git reset --hard <hash>.

    5

    Can reflog recover edits you never committed?

    Show answer

    No — only states that were committed (or stashed).