Lesson 6 of 8 in Git and collaboration

Undoing mistakes safely

Core12 minWritten for Git 2.28 or newer

Undoing in git means one of three things: discarding edits that are not committed, moving a branch back to an earlier commit, or adding a new commit that cancels an old one. Which one is safe depends on whether the work was committed and whether anyone else has it.

Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.

Read these first: Remotes, fetch, push and pull requests

The problem this solves

Everyone commits to the wrong branch, stages a password, or deletes a file they wanted. Git offers many ways to undo, and tutorials list them as a flat set of commands. People then run a destructive one at the wrong moment.

Two questions sort the options. First, was the work ever committed? Uncommitted edits that you discard are gone for good. Committed work is very hard to lose. Second, has anyone else received the commits? Rewriting history that others already have causes trouble for the whole team.

This is also where git's safety net is. Because commits are never edited, a commit you moved away from usually still exists and can be found again for a while.

How it works

Pick the undo tool by what you are undoing
Pick the undo tool by what you are undoingRestore discards uncommitted edits, reset moves a local branch back, and revert adds a new commit that cancels a shared one.overwrites filesmoves the branchadds an undo commitgit restoregit resetgit revertUncommitted editsgone for good if discardedLocal commitsrecoverable with reflogPushed commitsothers may have them
  • Control
  • Runs code
  • Data

Restore discards uncommitted edits, reset moves a local branch back, and revert adds a new commit that cancels a shared one.

  1. git restore to Uncommitted edits: overwrites files
  2. git reset to Local commits: moves the branch
  3. git revert to Pushed commits: adds an undo commit
The reflog remembers where the branch was
The reflog remembers where the branch wasAfter a hard reset moves main back to commit A, commits B and C still exist and the reflog still points at C, so a second reset can bring main back.parentparent1 now points here2 still remembersCommit ACommit BCommit Cmainafter reset --hard Areflogyour move history
  • Data
  • Control
  • Other

After a hard reset moves main back to commit A, commits B and C still exist and the reflog still points at C, so a second reset can bring main back.

  1. Commit B to Commit A: parent
  2. Commit C to Commit B: parent
  3. main to Commit A: 1 now points here
  4. reflog to Commit C: 2 still remembers

git restore <file> overwrites a file in your working tree with the staged or committed version. Anything you had not committed or staged is gone, and git cannot bring it back. git restore --staged <file> is gentler: it only unstages the file and leaves your edits alone.

git reset moves the current branch label to another commit. With --hard it also makes the files match, which discards uncommitted edits. Use it on commits you have not pushed. git revert <commit> leaves history alone and adds a new commit with the opposite changes. Because nothing is rewritten, it is the right undo for commits that others already have.

The reflog is a private journal of where HEAD and your branches pointed, kept in your local repository. Entries expire after a while: by default 90 days for commits still reachable and 30 days for unreachable ones. In the second diagram, main moved back to A, yet C still exists and the reflog names it, so you can move main back.

Try it yourself

Practice in a throwaway repository with three commits. Never run git reset --hard in a repository holding work you care about unless it is committed.

  1. 1
    Set up three commits
    mkdir git-lab-5 && cd git-lab-5
    git init -b main
    git config user.name "Your Name"
    git config user.email "you@example.com"
    echo "one" > a.txt && git add a.txt && git commit -m "One"
    echo "two" >> a.txt && git commit -am "Two"
    echo "three" >> a.txt && git commit -am "Three"
    git log --oneline
  2. 2
    Discard an uncommitted edit
    echo "oops" >> a.txt
    cat a.txt
    git restore a.txt
    cat a.txt

    The second cat no longer shows the oops line. This is the destructive undo: nothing remembers that line.

  3. 3
    Unstage without losing the edit
    echo "keep me" >> a.txt
    git add a.txt
    git restore --staged a.txt
    git status -s

    You should see

     M a.txt

    The edit is back to unstaged and still in the file. Finish by running git restore a.txt to drop it.

  4. 4
    Move the branch back, then recover it
    git reset --hard HEAD~1
    git log --oneline
    git reflog
    git reset --hard 'HEAD@{1}'
    git log --oneline

    The first reset drops commit Three from the branch. git reflog lists the move, and HEAD@{1} means where HEAD was one move ago. The second reset puts the branch back. The quotes stop some shells from reading the braces.

  5. 5
    Undo a commit without rewriting history
    git revert HEAD --no-edit
    git log --oneline
    cat a.txt

    A new commit appears on top. The old one is still in the history, but its change is cancelled. This is what you do for commits that are already pushed.

What breaks, and where to look

  • After git reset --hard or git restore ., your uncommitted edits are gone and git reflog does not show them.

    WHY IT HAPPENS
    The reflog records where branches and HEAD pointed. It does not record edits that were never committed.
    CHECK FIRST
    Run git reflog anyway to see whether the edits were ever committed. If you had run git add on them, git fsck --lost-found may find the staged content as dangling objects. Otherwise check your editor's local history.
  • ! [rejected] main -> main (non-fast-forward) after you reset or amended a commit that was already pushed.

    WHY IT HAPPENS
    You rewrote history that exists on the remote. Your branch no longer contains the commits the remote has, so the push looks like it would erase them.
    CHECK FIRST
    Run git log origin/main..main and git log main..origin/main. If teammates may have the old commits, undo with git revert instead of forcing. If forcing is truly needed on your own branch, use git push --force-with-lease, which stops if someone else pushed in the meantime.
  • error: could not revert <short-id>... <commit message>

    WHY IT HAPPENS
    Later commits changed the same lines, so reversing the old commit conflicts with current content.
    CHECK FIRST
    Run git status, resolve the conflict as in the previous concept, git add the file, then run git revert --continue. To cancel, run git revert --abort.

When not to use this

  • Do not rewrite commits that others have pulled. Prefer git revert on shared branches such as main.
  • A reflog entry is local and eventually expires. Treat it as a short-term rescue, not as a backup. Push work you care about to a remote.

Check yourself

Which undo cannot be recovered if you change your mind?

Discarding uncommitted edits, for example with git restore <file> or git reset --hard. Git never saved those edits.

Why use revert instead of reset on a pushed commit?

Revert adds a new commit and rewrites nothing, so teammates who already have the old commit stay in sync.

How do you find a commit after resetting past it?

Run git reflog, find the entry where the branch pointed at it, and reset or switch back to that entry such as HEAD@{1}.

Lesson 36 of 92 in the DevOps path

Search

Search cheat sheets and projects