To undo modifications in a file tracked by Git and restore it to the last committed state, you can use the following command:
git checkout -- <file-name>
Replace `<file-name>` with the name of the file you want to revert.
Understanding the Modified Files in Git
What Are Modified Files?
Modified files are those that have been changed in your working directory since the last commit. In Git, there are two main categories of files: tracked and untracked.
-
Tracked files are those that Git is aware of, having been added to the repository with `git add` at some point. These files can be:
- Unmodified: The file hasn’t changed since the last commit.
- Modified: The file has changed in the working directory but hasn't been staged for commit.
- Staged: The changes have been staged, ready to be committed.
-
Untracked files are files in your working directory that Git hasn’t been told to track yet. These files won’t be included in commits until you add them to Git.
Why You May Need to Undo Changes
There are several common scenarios where knowing how to git undo modified files is crucial:
- Accidental Modifications: Maybe you edited a file and unintentionally created bugs.
- Wrong Implementations: You tried a new feature that didn’t work as expected.
- Pure Experimentation: You were testing a piece of code and now want to restore stability.

Methods to Undo Modified Files
Undoing Changes in Tracked Files
Using `git checkout`
One way to revert changes made to a file is to use the `git checkout` command. This command can restore the last committed state of a modified file, effectively undoing any of your recent changes.
Code Snippet:
git checkout -- <filename>
Explanation: The double dash (`--`) is used to differentiate between options and filenames. By doing this, Git will ignore any command-line options that follow it and treat the next argument as a file name. This command will revert the specified file back to its last committed state.
Examples: If you've edited `example.txt` and want to discard those changes, simply run:
git checkout -- example.txt
Using `git restore`
The `git restore` command is a newer addition since Git version 2.23 and offers a more intuitive way to undo changes.
Code Snippet:
git restore <filename>
Explanation: The `restore` command is designed specifically for restoring files, which makes it clear and intentional compared to earlier commands. Using `git restore`, you can bring a modified file back to its last committed state without ambiguity.
Examples: For instance, if `example.txt` has changes you wish to discard, run:
git restore example.txt
Notice how `git restore` makes it clearer that you are restoring a file, while `checkout` may imply changing branches or other functionalities.
Undoing Changes to Staged Files
Using `git reset`
When you've staged a file for commit but later decide to make changes, you can unstage it using `git reset`. This command moves changes from the staging area back to the working directory.
Code Snippet:
git reset <filename>
Explanation: This command removes the specified file from the staging area. The changes remain in your working directory, allowing you to edit them further or discard them later.
Examples: If you added a file using `git add` and want to unstage it:
git add example.txt
git reset example.txt
Discarding All Changes
Resetting the Entire Working Directory
If you're looking to revert all modifications made to your working directory, you can use the following commands, but be cautious. This action can lead to irreversible loss of any uncommitted work.
Code Snippet:
git checkout -- .
or
git restore .
Explanation: Both commands will revert every modified file in the directory back to their last committed state.

Handling Uncommitted Changes
Using `git stash`
Sometimes, you may have changes that you want to save temporarily without committing them. This is where `git stash` comes in handy.
Code Snippet:
git stash
Explanation: `git stash` takes your modified and staged tracked files and stores them in a stack for later use. This helps you switch branches or work on another task without losing your progress.
After you've stashed your changes, you can see the list of stashed items using:
git stash list
To restore your stashed changes back to the working directory, use:
git stash pop
Examples: If you’re working on multiple features and need to temporarily shelve your modifications, using `git stash` keeps your workspace clean until you're ready to return.

Conclusion
Mastering how to git undo modified files is essential for smooth and effective version control. By understanding the differences between tracked and untracked files, recognizing scenarios that require reverting changes, and knowing the right commands to use, you can maintain the integrity of your code easily.
As best practices, consider committing often with descriptive messages to facilitate better management of your project's history. With a bit of practice, you'll become proficient in using Git commands to manage your files effectively, ensuring a seamless workflow. Remember, experimentation is part of development; knowing how to revert changes is key to maintaining stability.