Skip to content
jsworkbench.
Learning path
Intermediate18 min

Undoing changes

Understand the idea

Revert adds a new commit that undoes a change. Reset moves a branch label and can throw shared history away.

The idea

git revert adds a that undoes an earlier change and leaves history in place. git reset moves a label. --soft keeps the index and files, --mixed keeps the files and clears the index, --hard makes the files match the commit you named. git stash parks a dirty tree so you can switch branches without committing it.

What changes

Revert changes history by adding a node. Reset changes which node the label names, and maybe the . git restore copies a file back from the index or from HEAD without moving the branch. A later push --force is the dangerous version of reset: it moves the remote label even when that drops commits. A normal push refuses that case.

Ways to undo
CommandHistoryFiles
git revertNew commit, old one staysThe change is reversed
git reset --softLabel moves backIndex and files stay
git reset --hardLabel moves backFiles match the target
git stashNo new commitDirty edits are set aside

The command

Change a file, stash it, and run git stash list then git stash pop. On a commit you have not pushed, try git reset --mixed HEAD only when you mean to uncommit. Prefer git revert HEAD once the commit might be shared. The force-push article linked below is the remote form of this same choice.

The mistake

Check your graph

After revert, the old commit is still on the graph and a new commit sits on top. After reset, the branch label has moved and git reflog still names the commit you left.

Your repository

Stash a dirty tree, then compare reset and revert on a commit you have not pushed.

  1. Step 1

    Make a dirty edit

    In the editor, add this line to README.md: temporary note

  2. Step 2

    Park the edit

    Stash sets the working tree back to the commit and keeps your edit in the stash list.

  3. Step 3

    See the parked edit

    The list is the shelf. The branch did not move.

  4. Step 4

    Bring the edit back

    pop restores the working tree. The editor shows the temporary line again.

  5. Step 5

    Stage the note

    The popped edit is back in the working tree. Add it before the commit.

  6. Step 6

    Commit something you can undo

    This commit is the one revert will reverse.

  7. Step 7

    Undo with a new commit

    Revert adds a commit that reverses the last change. The old commit stays on the graph.

  8. Step 8

    See both commits

    The original commit and the revert are both still there.

The editor edits the working tree. Start a repository and the files appear here.

Follow the steps. Each command step can be placed in the terminal. Edit files in the editor, then stage them.
Supported command reference
  • init
  • add <path> | add .
  • commit -m "message" | commit --amend -m "message"
  • status
  • diff | diff --staged
  • log | log --oneline | log --graph | log --oneline --graph
  • show [ref]
  • branch | branch <name> | branch -d <name>
  • checkout <ref> | checkout -b <name> | switch <name> | switch -c <name>
  • merge <branch>
  • rebase <branch> | rebase -i <branch> | rebase --edit-todo <pick|drop|reword> <hash> [message] | rebase --continue | rebase --abort
  • cherry-pick <ref>
  • reset --soft <ref> | reset --mixed <ref> | reset --hard <ref>
  • revert <ref>
  • stash | stash list | stash pop | stash apply
  • restore <path> | restore --staged <path>
  • remote | remote add <name> <repo-slug>
  • clone <repo-slug> | git clone <repo-slug> | git fork <repo-slug>
  • fetch [remote] | pull [remote] [branch] | push [remote] [branch] | push --force [remote] [branch]
  • blame <path>
  • reflog
  • tag | tag <name> [ref] | tag -d <name>
  • bisect start | bisect good [ref] | bisect bad [ref] | bisect reset
  • config <key> <value> | config --list
  • write <path> <text> (workbench: edit the working tree)
  • issue new "title" | pr new <source> <target> "title" | review "comment" | review --request-changes "comment"

Commit graph

No commits yet. git init creates the first one.

Quiz

A quick check for understanding. Retry as often as you like; your learning path stays open.

01How does revert undo a commit?
02What does push --force do to a remote branch?

Ready to call this one understood?

Mark this lesson complete to earn 150 XP. You can always revisit it.

Sign in to save progress
Next lesson