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 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?

Answers


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'

git reset HEAD
git checkout .

git reset HEAD will unstage all changes and git checkout . discards all the changes.


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.


Need Your Help

Eclipse MyFaces Wizard?

eclipse plugins wizard myfaces

I am new to java development and a customer uses MyFaces for MVC based website development. While I know the ASP.NET based MVC Framework from Microsoft and the possibility of easily adding new page...

How to change background image in hover?

css image background hover

The image file which I'm working with is a .png file created out of two identical objects in different colors (blue/orange). The objects in the .png file are next to eachother. What I want to do is...