Git Tutorial menu

Git Undoing Changes

Everyone makes mistakes. Git gives you safe ways to undo them, depending on how far the change has gone.

Which Command Should I Use?

SituationCommand
I edited a file and want the last committed version backgit restore file
I staged a file by mistakegit restore --staged file
My last commit message is wrong, or I forgot a filegit commit --amend
I want to undo a commit that is already sharedgit revert <commit>
I want to remove my last local commitsgit reset <commit>

Discard Changes in a File

You broke index.html and want to go back to the last commit:

Example

bash
git restore index.html

Be careful: this throws away your uncommitted edits to that file for good. Git cannot bring them back, because they were never committed.

Unstage a File

You ran git add . but did not want to include notes.txt:

Example

bash
git restore --staged notes.txt
git status
Output
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   index.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	notes.txt

The file itself is not changed; it is just taken out of the next commit.

Fix the Last Commit

Forgot a file, or made a typo in the message? If you have not pushed yet:

Example

bash
git add contact.html
git commit --amend -m "Add contact page and form"

This replaces the last commit with a new, corrected one.

git revert: The Safe Undo

git revert creates a new commit that does the opposite of an old one. History is kept, so it is safe even after you shared your work on GitHub.

Example

bash
git revert a91c3e7
git log --oneline
Output
d4e5f60 (HEAD -> main) Revert "Change heading colour to red"
a91c3e7 Change heading colour to red
8b41d07 Add basic stylesheet
3f2a9c1 Add home page

Git opens an editor for the message (save and close it to accept). The log then shows a new commit on top that undoes a91c3e7.

git reset: Move Back in History

git reset moves your branch back to an older commit, as if the later commits never happened. It has three modes:

ModeCommits removedStaging areaYour files
--softYesChanges kept stagedKept
--mixed (default)YesChanges unstagedKept
--hardYesClearedChanges deleted

Example

bash
# Undo the last commit but keep the changes in your files
git reset HEAD~1

Example

bash
# Throw away the last commit AND its changes completely
git reset --hard HEAD~1

Only use git reset on commits you have not pushed. If others already have those commits, use git revert instead, or you will cause problems for your team.

Going Back to See an Old Version

To look at the project as it was in an old commit, without changing anything:

Example

bash
git switch --detach 3f2a9c1

Look around, then come back to the latest version:

Example

bash
git switch main

Exercise

Exercise

You pushed a bad commit to GitHub and your team already pulled it. What is the safest way to undo it?

Learn Git with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp