<!--order=6-->
<h1>Git: Undoing Changes</h1>
<p>Git gives you multiple ways to undo things depending on where in the workflow the mistake occurred — in the working tree, in the staging area, in the last commit, or deeper in history. Knowing which tool to reach for (and what the consequences are) is critical. Some undos are safe and reversible; others rewrite history and can cause problems if others have seen those commits.</p>

<h2>Commands Covered</h2>
<p><code>git revert</code> · <code>git reset</code> · <code>git restore</code> · <code>git clean</code></p>

<h2>Undo Decision Tree</h2>
<table>
  <thead><tr><th>Where is the mistake?</th><th>Command</th><th>Safe to use on pushed commits?</th></tr></thead>
  <tbody>
    <tr><td>Working tree (unsaved edit)</td><td><code>git restore &lt;file&gt;</code></td><td>Yes — local only</td></tr>
    <tr><td>Staging area (git add'd but not committed)</td><td><code>git restore --staged &lt;file&gt;</code></td><td>Yes — local only</td></tr>
    <tr><td>Last commit (not yet pushed)</td><td><code>git commit --amend</code></td><td>No — rewrites history</td></tr>
    <tr><td>Last commit (already pushed)</td><td><code>git revert HEAD</code></td><td>Yes — creates a new commit</td></tr>
    <tr><td>Multiple old commits (not pushed)</td><td><code>git reset</code></td><td>No — rewrites history</td></tr>
    <tr><td>Multiple old commits (pushed)</td><td><code>git revert</code> each commit</td><td>Yes — creates new commits</td></tr>
    <tr><td>Untracked files (not committed)</td><td><code>git clean</code></td><td>Yes — local only; irreversible</td></tr>
  </tbody>
</table>

<h2>git revert</h2>
<p>Creates a new commit that undoes the changes from a previous commit. The original commit stays in history — nothing is erased. This is the safe way to undo pushed commits.</p>
<pre>
# Revert the most recent commit
$ git revert HEAD

# Revert a specific commit (by hash)
$ git revert abc1234

# Revert without auto-committing (stage the reversal for review)
$ git revert --no-commit abc1234
$ git revert -n abc1234
# Then: git commit -m "Revert the foo feature"

# Revert a range of commits (reverting oldest first is usually safer)
$ git revert abc1234..def5678
# This reverts all commits between abc1234 and def5678 (exclusive of abc1234)

# Revert a merge commit (must specify which parent to revert to)
$ git revert -m 1 abc1234   # -m 1 = keep the first parent (main branch side)
</pre>

<h2>git reset</h2>
<p>Moves the current branch pointer backward to an earlier commit. Has three modes controlling what happens to the changes between the old and new HEAD.</p>
<pre>
# Soft reset — move HEAD back, keep changes staged
$ git reset --soft HEAD~1
# The last commit is undone; files are staged as they were just before commit.
# Useful: you committed too soon, want to restructure the commit.

# Mixed reset (default) — move HEAD back, unstage changes, keep in working tree
$ git reset HEAD~1
$ git reset --mixed HEAD~1
# The last commit is undone; changes are in your working tree but unstaged.
# Useful: undo a commit and re-stage things differently.

# Hard reset — move HEAD back, discard all changes completely
$ git reset --hard HEAD~1
# WARNING: changes are GONE — not in staging area, not in working tree.
# (Can be recovered with git reflog if you're quick)

# Reset to a specific commit
$ git reset --hard abc1234

# Unstage a file (without touching HEAD) — same as git restore --staged
$ git reset HEAD myfile.c
$ git reset -- myfile.c

# Unstage everything (keep changes in working tree)
$ git reset HEAD
</pre>

<h3>--soft vs --mixed vs --hard</h3>
<table>
  <thead><tr><th>Flag</th><th>HEAD moves?</th><th>Index (staged)?</th><th>Working tree?</th></tr></thead>
  <tbody>
    <tr><td><code>--soft</code></td><td>Yes</td><td>Unchanged (still staged)</td><td>Unchanged</td></tr>
    <tr><td><code>--mixed</code> (default)</td><td>Yes</td><td>Reset (unstaged)</td><td>Unchanged</td></tr>
    <tr><td><code>--hard</code></td><td>Yes</td><td>Reset</td><td>Reset (changes lost)</td></tr>
  </tbody>
</table>

<pre>
# Recovering from a bad hard reset using reflog:
$ git reflog
# abc1234 HEAD@{0}: reset: moving to HEAD~3
# def5678 HEAD@{1}: commit: The commit I accidentally removed

$ git reset --hard def5678   # restore to before the reset
# (The reflog keeps entries for ~90 days by default)
</pre>

<h2>git restore</h2>
<p>(Covered more fully in the Staging article — key points for undoing:)</p>
<pre>
# Discard changes in a file (revert to last committed state)
$ git restore myfile.c
# This is destructive — unsaved changes are gone

# Unstage a file (keep changes in working tree)
$ git restore --staged myfile.c

# Restore a file to the state it was in 3 commits ago
$ git restore --source=HEAD~3 myfile.c
</pre>

<h2>git clean</h2>
<p>Removes untracked files from the working tree. Useful after a build that left artifacts everywhere, or to reset to a clean state. <strong>Irreversible</strong> — files are deleted, not moved to trash.</p>
<pre>
# Show what would be deleted (dry run — always do this first)
$ git clean -n
$ git clean --dry-run

# Delete untracked files (requires -f, a safety measure)
$ git clean -f

# Delete untracked files AND untracked directories
$ git clean -fd

# Delete untracked files including ignored files (e.g. build output)
$ git clean -fx

# Delete everything untracked including directories and ignored files
$ git clean -fdx
# WARNING: this is the nuclear option — use git clean -n first

# Interactive mode (confirm each file)
$ git clean -i
$ git clean --interactive

# Clean only a specific directory
$ git clean -f src/
</pre>

<h2>Amending the Last Commit</h2>
<pre>
# Fix the message of the last commit
$ git commit --amend -m "Better message"

# Add a forgotten file to the last commit
$ git add forgotten-file.txt
$ git commit --amend --no-edit   # reuse existing commit message

# Amend the author
$ git commit --amend --author="New Name &lt;new@email.com&gt;"

# WARNING: amend creates a new commit object — the old commit SHA is gone.
# If you've already pushed, you'll need --force-with-lease to push.
# Don't amend shared commits that others have already pulled.
</pre>

<h2>References</h2>
<ul>
  <li><a href="https://git-scm.com/docs/git-revert" target="_blank" rel="noopener">git-revert documentation ↗</a></li>
  <li><a href="https://git-scm.com/docs/git-reset" target="_blank" rel="noopener">git-reset documentation ↗</a></li>
  <li><a href="https://git-scm.com/docs/git-clean" target="_blank" rel="noopener">git-clean documentation ↗</a></li>
  <li><a href="https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified" target="_blank" rel="noopener">Pro Git Book — Reset Demystified ↗</a></li>
</ul>
