What's the quickest way to undo changes (staged and unstaged) in Git?
Both files unstaged.
$ git status -s M file1.txt # unstaged ?? oops.txt # unstaged
One file staged, one file unstaged.
$ git status -s M file1.txt # staged ?? oops.txt # unstaged
I can add
all to index and then stash
save
and drop
.
$ git add . $ git stash save $ git stash drop $ git status nothing to commit, working directory clean
Is there a quicker way?
Discard Staged Changes With Git Reset We can use git reset with --hard to discard staged files. After running the git reset command, all the changes of the staged files including the newly created and existing files will be discarded.
Staged files are those which go into your next commit. If you accidentally added files to the staged area, you can undo this by typing git restore --staged <file> , so in this case, it would be git restore --staged lib.
For all unstaged files in current working directory use: git restore . That together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation. If a file has both staged and unstaged changes, only the unstaged changes shown in git diff are reverted.
git reset HEAD git checkout .
git reset HEAD
will unstage all changes and git checkout .
discards all the changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With