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.


Learn more about the similar topics:
Tutorials
Git Restore
Git Commit Amend
Git Reset
Git Revert
Git Stash
Git Reflog
Exercises & Assignments
Discard the changes of a file
Undo the last commit but keep the changes
Revert a commit with git revert
Fix the message of the last commit
Stash your changes and bring them back
Recover a deleted branch with git reflog
Interview Questions & Answers
What is git revert?
How to undo last commit using git reset?
What is the difference between git reset and git revert?
What is the difference between soft, mixed and hard reset in Git?
What is git stash and when do you use it?
How do you change the last commit message in Git?
How do you squash commits in Git?