Git Undo Changes

Unstage a file

Description: You staged secret.txt by mistake. Remove it from the staging area, but keep the file.

Hint:

  • Use git restore --staged.

Solution

Terminal output

$ git status -s
A secret.txt
$ git restore –staged secret.txt
$ git status -s
?? secret.txt
$ ls
app.txt
secret.txt

How it works

  • A (staged) changes to ?? (untracked), and the file stays in the folder.
  • Add the file to .gitignore so this does not happen again.

Read Git Restore.

Discard the changes of a file

Description: You edited config.txt and broke it. Discard your edit and get the last saved version back.

Hint:

  • Use git restore file. It cannot be undone, so be sure.

Solution

Terminal output

$ git status -s
M config.txt
$ git restore config.txt
$ git status -s
$ cat config.txt
theme=light

How it works

  • The file is replaced with the version from the last commit.
  • Changes that were never committed are lost, Git cannot bring them back.

Read Git Restore.

Undo the last commit but keep the changes

Description: You committed too early. Undo the last commit, but keep your changes ready in the staging area.

Hint:

  • HEAD~1 means one commit before HEAD.

Solution

Terminal output

$ git reset –soft HEAD~1
$ git log –oneline
2875ee0 (HEAD -> main) First
$ git status -s
M f.txt

How it works

  • The branch moves back one commit, and the commit “Second” is gone from the history.
  • M shows that the change is still staged, so you can finish your work and commit again.

Read Git Reset.

Revert a commit with git revert

Description: The commit “Add debug page” was a mistake, and it is already pushed. Undo it without changing the history.

Hint:

  • Use git revert with --no-edit to accept the default message.

Solution

Terminal output

$ git revert HEAD~1 –no-edit
[main a51ba50] Revert "Add debug page"
Date: Sat Jan 10 11:31:00 2026 +0000
1 file changed, 1 deletion(-)
delete mode 100644 debug.txt
$ git log –oneline
a51ba50 (HEAD -> main) Revert "Add debug page"
27e27e3 Add about page
f5f5ec5 Add debug page
95d0194 Add home page
$ ls
about.txt
home.txt

How it works

  • Git made a new commit which removes debug.txt.
  • The old commits are still in the history, so it is safe for shared branches.

Read Git Revert.

Fix the message of the last commit

Description: The last commit message is “Add contcat page”. Change it to “Add contact page”.

Hint:

  • Use git commit --amend -m.

Solution

Terminal output

$ git log –oneline
0e698a4 (HEAD -> main) Add contcat page
$ git commit –amend -m "Add contact page"
[main 6e54967] Add contact page
Date: Sat Jan 10 10:42:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 contact.html
$ git log –oneline
6e54967 (HEAD -> main) Add contact page

How it works

  • Amend replaces the last commit with a new commit, so the hash changes.
  • Do not amend a commit that is already pushed and used by others.

Read Git Commit Amend.

Stash your changes and bring them back

Description: You edited app.txt but must switch tasks. Put the changes aside, check that the folder is clean, and then bring the changes back.

Hint:

  • Use git stash and git stash pop.

Solution

Terminal output

$ echo "half done" >> app.txt
$ git stash
Saved working directory and index state WIP on main: 6b3a366 Add app
$ git status -s
$ git stash pop
On branch main
Changes not staged for commit:
(use "git add <file>…" to update what will be committed)
(use "git restore <file>…" to discard changes in working directory)
modified: app.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (0aac64f7d5bb638391472f0c078c4e2ca0506864)
$ cat app.txt
v1
half done

How it works

  • After git stash the folder is clean, so you can switch branches.
  • git stash pop applies the changes again and removes the stash entry.

Read Git Stash.