Git Intermediate: Undo, Collaborate and Resolve
Undoing Changes Safely
In this lesson you will
- Choose the right command to undo each kind of mistake
- Explain the difference between revert and reset
- Know when rewriting history is unsafe
Introduction
Everyone makes mistakes in Git. The good news is that nearly all of them can be undone, if you pick the right tool. The key question is: where is the mistake? In your files, in the staging area, in a local commit, or in a commit you already shared?
The simple explanation
For private work you can rewrite history. For shared work, add a new commit that undoes the old one.
Mistake in your files (not committed)
Throw away changes to one file and go back to the last commit:
Terminal
git restore notes.txtThis permanently discards your edits to that file, so be sure.
Mistake in the staging area
Unstage a file but keep your edits:
Terminal
git restore --staged notes.txtMistake in the last commit
Forgot a file or made a typo in the message? Amend the last commit:
Terminal
git add forgotten.txt
git commit --amend -m "Add notes and forgotten file"Amending replaces the last commit with a new one. Do this only if you have not pushed it yet.
Undo a commit you already shared: revert
git revert makes a new commit that does the opposite of an earlier one. History stays intact, so it is safe on shared branches.
Terminal
git revert a1b2c3dReplace a1b2c3d with the commit's hash from git log --oneline.
Quick quiz
A bad commit is already on a shared main branch. What is the safest way to undo it?
Move back in history: reset
git reset moves the current branch to an earlier commit. It has three common modes:
| Command | Branch moves | Staging area | Your files |
|---|---|---|---|
git reset --soft HEAD~1 | Yes | Keeps changes staged | Kept |
git reset HEAD~1 (mixed) | Yes | Unstaged | Kept |
git reset --hard HEAD~1 | Yes | Cleared | Discarded |
HEAD~1 means "one commit before the current one". --hard can destroy uncommitted work, so use it carefully.
Reset rewrites history. Use it on local work only. If you reset commits that others already have, their history and yours diverge and it causes trouble.
Safety net: the reflog
Git remembers where your branch has been for a while. If you reset too far:
Terminal
git reflog
git reset --hard HEAD@{2}git reflog lists recent positions of HEAD. Find the one you want and reset back to it. It works only on your own computer and for a limited time.
Try it yourself
In a practice repository, make two commits. Use git revert on the last one and look at git log --oneline. Then use git reset --soft HEAD~1 on a different practice commit and notice your changes are still staged.
Summary
restorefixes files and the staging area, andcommit --amendfixes the last commit.revertsafely undoes shared commits by adding a new one.resetmoves a branch and rewrites history, so use it only on private work, withreflogas a safety net.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading