Git Cheat Sheet · Changes
Discard changes
git restore <file>
To discard a file's changes and go back to its last recorded version, run git restore <file>. Be careful, this is final: Git keeps no copy of uncommitted changes.
What it does
Puts a file back in the state of the index: unstaged changes are discarded.
A use case
You tried things out in a file and want to start again from the last clean version.
Warning: Irreversible: the changes are lost and an untracked file is deleted from disk.
On the command line
git restore <file>- Puts the file back in the state of the index.
git restore .- Discards all unstaged changes in the current folder.
git restore --source=HEAD --staged --worktree <file>- Goes back to the last commit, staged or not.
git restore -p <file>- Discards only some blocks.
git checkout -- <file>- Older syntax, equivalent to
git restore. git clean -n then git clean -f- Lists, then deletes, untracked files.
In Merathon
- Hover the file in "Unstaged" and click the back arrow ("Discard changes"), or right-click → "Discard changes…".
- From the diff, "Discard hunk" only discards part of the file.
Frequently asked questions
Can I get back changes discarded by git restore?
No, if they were never staged or committed. If you are not sure, set them aside with git stash instead.
What is the difference with git reset --hard?
git restore acts on files. git reset --hard resets the whole working copy, and can move the branch to another commit.