47

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?

Kara
  • 6,115
  • 16
  • 50
  • 57
hIpPy
  • 4,649
  • 6
  • 51
  • 65

5 Answers5

55
git reset HEAD
git checkout .

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

Nima
  • 3,129
  • 1
  • 21
  • 20
40

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'
Community
  • 1
  • 1
William Seiti Mizuta
  • 7,669
  • 3
  • 31
  • 23
  • 2
    i added an alias `undo = !"git reset --hard && git clean -f"` – hIpPy Feb 05 '14 at 18:05
  • 3
    nice.. and if you're wondering where the bang comes from: http://stackoverflow.com/questions/10641451/what-is-the-meaning-of-the-bang-or-before-the-git-command – Calaf Jan 25 '17 at 23:12
  • 17
    This answer uses incorrect terminology. `git reset --hard` **throws away all changes, staged in the index and unstaged in the working tree**. `git clean -fd` removes all files and directories **that are not tracked**. For instance anything locally built like `.o` files, `config.make` from a configure script, your `tags`, and so on. – Kaz Sep 21 '17 at 22:13
  • Untracked files are not always considered to be "changes". `git clean` is dangerous, which it why it has `-f` for safety!!! – Kaz Sep 21 '17 at 22:21
  • 3
    What's "!" for? – Philip Rego Jan 30 '19 at 17:49
  • "undo" is a terrible alias for all that destruction – cambunctious Sep 23 '19 at 17:07
  • 4
    @PhilipRego `!` makes it a shell command instead of just arguments to git. In this case it is used to run git multiple times. – cambunctious Sep 23 '19 at 17:08
  • 1
    @Kaz is right. Be careful while using `git reset --hard` . I used it before my initial commit and lost all my work, then had to redo them all. This command discards (removes) all your local changes and resets project to last commit on *remote*. – Bakhtiyor Sulaymonov Jun 15 '21 at 06:22
  • @bakhti_UZB I've lost edits to `git reset --hard` a few times in the past, including fairly recently; but I recovered them thanks to editor backups! – Kaz Jun 15 '21 at 06:30
  • @Kaz It happened recently on vscode with me. I could recover my files using method on this post https://medium.com/@CarrieGuss/how-to-recover-from-a-git-hard-reset-b830b5e3f60c . But there were files from *node-modules* folder and file names were not understandable. Then I thought it would be easier to redo one day work than searching files from recovery folder. And did so ) – Bakhtiyor Sulaymonov Jun 15 '21 at 19:42
15

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.

CostaIvo
  • 1,029
  • 13
  • 19
8

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.

Zombo
  • 1
  • 62
  • 391
  • 407
-7

git rm --cached -r .

//will Unstaged all the staged/tracked files in git

Bhaskar Kumar
  • 17
  • 1
  • 5