How to revert local changes to a file in Git
Summary
Learn how to undo changes in Git using commands like git restore
, git reset
, and git checkout
. This guide clearly explains how to manage both staged and unstaged file changes in your project workflow.
Introduction #
When working with Git, it is common to make changes you later want to discard. Whether you are fixing a mistake or experimenting, knowing how to revert local changes is important for maintaining a clean and functional working directory.
Understanding staged and unstaged files #
Git tracks changes to your files in stages.
When you modify a file but do not run git add <file>
, the change is considered unstaged.
Once you run git add
, the file becomes staged and is ready to be included in the next commit.
Use the command below to view the status of your files:
git status
This command shows you which files are staged for commit, which are modified but unstaged, and which files are untracked.
How to revert unstaged changes with git restore
#
If you have made changes to a file but have not staged them, you can discard those changes using the git restore
command. This command will revert the file to its last committed state.
git restore <file>
For example, to discard changes to a file named main.py
, run:
git restore main.py
This will remove all modifications made to main.py
since the last commit, effectively restoring the file to its most recently committed version.
How to revert staged changes #
If you have already staged changes with git add
, you must first unstage them before you can restore the file to its committed state.
- Unstage the file:
git reset <file>
- Restore the file to the committed version:
git restore <file>
For example, to revert staged changes to main.py
, use:
git reset main.py
git restore main.py
This combination removes the file from the staging area and then reverts it to its last committed state.
For more reset
tips, see our article How to undo the latest local commit in Git.
Using git checkout
#
Before git restore
was introduced (Git ≥ 2.23), the common way to discard changes was with the git checkout
command:
git checkout -- <file>
Although this still works, it is now considered deprecated in favor of git restore
, which is more descriptive and safer to use.
FAQ's #
Most common questions and brief, easy-to-understand answers on the topic:
How do I see which files have been modified in Git?
Use the command git status
to view a list of modified, staged, and unstaged files.
What is the difference between staged and unstaged files in Git?
Unstaged files have been modified but are not marked for commit, while staged files have changes added to the staging area and are ready to be committed.
Can I undo changes to a specific file only?
Yes, you can use git restore <file>
for unstaged files or git reset <file>
followed by git restore <file>
for staged files.
Is git checkout -- <file>
still valid?
Yes, but it is considered deprecated for restore purposes. Use git restore
instead, which is more modern and clearer.
Further readings #
Sources and recommended, further resources on the topic:
- Git documentation: git status
- Git documentation: git restore
- Git documentation: git reset
- Git documentation: git checkout
- How to revert local changes to a file in Git
License
How to revert local changes to a file in Git by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/how-to-revert-changes-to-a-file-in-git">How to revert local changes to a file in Git</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>
For more information see the Ditig legal page.