Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

GIT
Git & GitHub
0/10 completed
Beginner
What is version control?Your first commitsBranchesGitHub and remotes
Intermediate
Merging and conflictsUndoing changesPull requests and code reviewRebasing
Advanced
Git internals
Pro
Git workflows for teams
Tracks / Git & GitHub / Intermediate

Undoing changes

Everyone makes mistakes: a typo in a commit message, a file committed by accident, a change that broke everything. Git can undo almost anything, if you pick the right tool. This lesson is your rescue guide.

Quick reference: "I want to…"

Situation Command Safe on shared branches?
Discard uncommitted edits to a file git restore <file> ✅ (local only)
Unstage a file (keep the edits) git restore --staged <file> ✅
Fix the last commit's message or add a forgotten file git commit --amend ⚠️ Only if not pushed yet
Undo the last commit, keep its changes staged git reset --soft HEAD~1 ⚠️ Only if not pushed yet
Undo the last commit, keep its changes unstaged git reset HEAD~1 ⚠️ Only if not pushed yet
Throw away the last commit and its changes git reset --hard HEAD~1 ⚠️ Destructive
Undo a commit that's already pushed git revert <commit> ✅ The safe way
Shelve work temporarily git stash / git stash pop ✅
Recover "lost" commits git reflog ✅

HEAD~1 means "one commit before HEAD", and HEAD~3 means three commits back.

Discarding and unstaging

Git terminal (simulator) · editable

⚠️ git restore <file> permanently discards uncommitted edits. There's no undo for work that was never committed.

git reset: move the branch backwards

reset moves your branch pointer to an earlier commit. The mode decides what happens to the changes from the commits you "removed":

          --soft    →  changes stay STAGED
(default) --mixed   →  changes stay in your files, UNSTAGED
          --hard    →  changes are DELETED
Git terminal (simulator) · editable

Try it again with git reset --hard HEAD~1 and notice that c.txt is gone (ls).

git revert: undo safely on shared history

If a commit is already pushed and shared, never rewrite history. Teammates would end up with conflicting histories. Instead, revert: Git creates a new commit that does the opposite.

Git terminal (simulator) · editable

The bad commit stays in history, but its effect is cancelled, and about.html from the later commit is untouched.

Amending the last commit

bash
git commit -m "Add lgoin page"          # typo!
git commit --amend -m "Add login page"  # replace the message

git add forgotten-file.js
git commit --amend --no-edit            # add a file to the last commit, keep the message

--amend replaces the last commit with a new one. Use it only on commits you haven't pushed.

The safety net: git reflog

Git records every place HEAD has been in the reflog, including commits you "deleted" with reset --hard. Real Git keeps these for about 90 days.

bash
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~1
# 9f8e7d6 HEAD@{1}: commit: The commit I thought I lost
git reset --hard 9f8e7d6     # it's back!
# or: git switch -c rescued 9f8e7d6

The rule: if it was ever committed, it can almost always be recovered. That's another reason to commit often.

Removing a file from Git but keeping it on disk

Committed .env or node_modules by mistake?

bash
echo ".env" >> .gitignore
git rm --cached .env          # stop tracking, keep the local file
git commit -m "Stop tracking .env"

(If it contained secrets, rotate them. They still exist in the history.)

Practice

  1. Make 4 commits. Use git reset --soft HEAD~2 to combine the last two into one new commit with a better message.
  2. Make a "bad" commit, then a good one after it, and use git revert to undo only the bad one.
  3. Explain to yourself why revert is safe for shared branches and reset isn't.
Git terminal (simulator) · editable
✎ Check your understanding

1.A bad commit was pushed to main and teammates have pulled it. How do you undo it?

2.What does git reset --soft HEAD~1 do?

← Merging and conflicts