How to Quickly Undo Staged and Unstaged Changes in Git?
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...
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?
5 Answers
git reset HEAD
git checkout .
git reset HEAD will unstage all changes and git checkout . discards all the changes.
You need to use two commands: git reset --hard and git clean -fd.git reset --hard will undo all staged changes and git clean -fd, unstaged changes (files and directories). You can create a alias that will do the two commands. For that, just add the following lines in your .gitconfig:
[alias]
undo = '!git reset --hard && git clean -fd'
You can use git clean
$ git status -s ?? oops.txt $ git clean -f Removing oops.txt $ git status -s
More info:
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
To unstage all the staged file use command:
git reset HEAD
To unstage a single file staged file (example: app.js) use command:
git reset HEAD app.js
With Git version 2.23.0 a new command git restore was introduced.
To unstage the staged file use:
git restore --staged app.js
This command will remove the file from staged area and move it back to the working directory with the changes intact.
To undo the changes in the file and restore it to its original contents use:
git restore app.js
Note: You will loose all the changes made it the file since the last commit.
git rm --cached -r .
//will Unstaged all the staged/tracked files in git