Categories
git version-control

How do I discard unstaged changes in Git?

5788

How do I discard changes in my working copy that are not in the index?

10

  • 15

    git-clean only removes untracked files from the working tree git-scm.com/docs/git-clean

    – Yega

    Sep 15, 2016 at 12:29

  • 32

    To clarify Asenar’s comment above, git-clean -df can be dangerous. It will delete local untracked files (e.g. covered by a .gitignore) Read all below carefully and consider git checkout . instead

    Oct 7, 2016 at 8:05


  • 24

    ‘git clean -df ‘ Be warned! I tried that and lost key folders that are unable to be restored… Ouch!

    Oct 27, 2016 at 21:01

  • 65

    hitting git status gives a suggestion on how to do that! git checkout -- .

    – Paulo

    Dec 21, 2017 at 10:42

  • 21

    @Paulo: starting in July 2019, git status gives the suggestion: git restore. git restore is a new command exactly for this purpose. See my 2019 update.

    – prosoitos

    Sep 12, 2019 at 20:39


3070

Another quicker way is:

git stash save --keep-index --include-untracked

You don’t need to include --include-untracked if you don’t want to be thorough about it.

After that, you can drop that stash with a git stash drop command if you like.

20

  • 12

    @KarimSamir: The question specifically asks about changes that are not in the index. The git reset command will discard changes in the index too.

    Apr 12, 2015 at 17:28

  • 171

    git checkout — . is much faster

    – Frank

    Apr 17, 2015 at 16:16

  • 43

    Neither the git stash, nor any variety of git checkout will discard unstaged deletes. According to the output of git status, the actual correct answer here is some flavor git reset HEAD

    May 27, 2015 at 22:27

  • 151

    This pollutes the stash stack. git checkout -- . does the job with one command only.

    Sep 9, 2015 at 11:17

  • 3

    @FelipeTonello I wish I could give you 100 rep for that comment. In my test, that seemed to do exactly what I wanted. Instead, I spent the last hour using “git checkout — *” and working around all the untracked file errors.

    Dec 11, 2015 at 3:36

3070

Another quicker way is:

git stash save --keep-index --include-untracked

You don’t need to include --include-untracked if you don’t want to be thorough about it.

After that, you can drop that stash with a git stash drop command if you like.

20

  • 12

    @KarimSamir: The question specifically asks about changes that are not in the index. The git reset command will discard changes in the index too.

    Apr 12, 2015 at 17:28

  • 171

    git checkout — . is much faster

    – Frank

    Apr 17, 2015 at 16:16

  • 43

    Neither the git stash, nor any variety of git checkout will discard unstaged deletes. According to the output of git status, the actual correct answer here is some flavor git reset HEAD

    May 27, 2015 at 22:27

  • 151

    This pollutes the stash stack. git checkout -- . does the job with one command only.

    Sep 9, 2015 at 11:17

  • 3

    @FelipeTonello I wish I could give you 100 rep for that comment. In my test, that seemed to do exactly what I wanted. Instead, I spent the last hour using “git checkout — *” and working around all the untracked file errors.

    Dec 11, 2015 at 3:36

2178

It seems like the complete solution is:

git clean -df
git checkout -- .

WARNING: while it won’t delete ignored files mentioned directly in .gitignore, git clean -df may delete ignored files residing in folders.

git clean removes all untracked files and git checkout clears all unstaged changes.

25

  • 147

    The other two answers don’t actually work, this one did.

    – John Hunt

    Sep 1, 2014 at 12:23

  • 19

    @dval this is becues the first command removed the unindexed files and the second one removed the unstaged changes (of indexed files). So if you did not have any staged changes this it is the same as reverting to the last commit with git reset --hard

    Oct 31, 2014 at 10:47

  • 3

    use -dff if the untracked directory is a git clone.

    – accuya

    Dec 16, 2014 at 2:52

  • 94

    Be careful running git clean -df. If you don’t understand what it does, you might be deleting files you mean to keep, like robots.txt, uploaded files, etc.

    – ctlockey

    Jan 28, 2015 at 14:57

  • 44

    As @ctlockey said, the first command also delete directories if they are composed of ignored files only… Lost a whole bunch of configuration files on my project 🙁 Be careful.

    Jul 16, 2015 at 8:00