Git Undo Changes

restore, reset, revert and amend.

✍️ 2 exercises in this lesson
  • git restore file: discard uncommitted changes in a file.
  • git restore --staged file: unstage a file (keep the changes).
  • git commit --amend: change the last commit (message or content). Only before pushing!
  • git revert <commit>: create a new commit that undoes an old one. Safe for shared history.
  • git reset --soft HEAD~1: undo the last commit but keep its changes staged.
git reset --hard permanently deletes uncommitted work. Use it with great care.

Key Takeaways: Git Undo Changes

Mastering Git Undo Changes helps you write clean and reliable Git code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Git Undo Changes Code Example

Git
git commit -m "Add pricng page"
git commit --amend -m "Add pricing page"

git revert 8c4d7e2 --no-edit
git log --oneline -3
Output
[main 5b7f0d1] Add pricing page
[main 9e2c6a3] Revert "Add homepage"
9e2c6a3 (HEAD -> main) Revert "Add homepage"
5b7f0d1 Add pricing page
1a9e3c4 Add contact page
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

Git Undo Changes Practice Exercises

Check what you learned in “Git Undo Changes”.

/ 2 solved
Fill in the blank

Create a new commit that undoes an old one (safe for shared history).

git  8c4d7e2
✓ Correct! Not quite. Try again.
Multiple choice

Which command permanently discards uncommitted work?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services