1224

Is there a Git command to revert all uncommitted changes in a working tree and index and to also remove newly created files and folders?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MEM
  • 30,529
  • 42
  • 121
  • 191
  • 2
    Possible duplicate of [How do you discard unstaged changes in Git?](http://stackoverflow.com/questions/52704/how-do-you-discard-unstaged-changes-in-git) – vusan May 13 '16 at 10:28
  • 2
    Well, I have read all of the varied and difficult to remember answers below, with their caveats and edge cases and "didnt work if you have xxx", and have stuck with deleting the entire repo, cloning it to remove all edited and added files. Is also only two commands. rm -r projectdir; git clone xxx. For me this is a frequent operation - check out a repo play around with it, then want to get back to a clean checkout so I can start modifying it. Not great, but works 100%. Hoping one day they will add a simple command for this. – John Little Aug 16 '18 at 22:07

14 Answers14

1982

You can run these two commands:

# Revert changes to modified files.
git reset --hard

# Remove all untracked files and directories.
# '-f' is force, '-d' is remove directories.
git clean -fd
James
  • 4,644
  • 5
  • 37
  • 48
htanata
  • 36,666
  • 8
  • 50
  • 57
  • 164
    good idea to run 'git clean -nd' to preview the changes before running git clean to ensure you dont have untracked files or directories that you care about that will be removed. – jpw Jul 14 '13 at 05:13
  • 87
    Save someone a trip to the docs: -f is force, -d is remove directories, -n is dry run (also --dry-run; show output without doing anything yet) – Aaron Campbell Jun 04 '15 at 21:07
  • 17
    `git clean -i` for an interactive mode. – galath Aug 12 '15 at 15:17
  • It didn't reset my unstaged files, I had to stage them first. – Aron Lorincz Jan 08 '16 at 12:06
  • This results in an error when merging: **fatal: You have not concluded your merge (MERGE_HEAD exists). Please, commit your changes before you merge.** – IgorGanapolsky Oct 12 '16 at 17:06
  • 5
    @IgorGanapolsky You're probably in the middle of merge conflict. Try running `git merge --abort`. – htanata Oct 12 '16 at 20:46
620

If you want to revert the changes only in the current working directory, use

git checkout -- .

And before that, you can list the files that will be reverted without actually making any action, just to check what will happen, with:

git checkout --
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramashish Baranwal
  • 7,166
  • 2
  • 18
  • 14
131

Use "git checkout -- ..." to discard changes in working directory

git checkout -- app/views/posts/index.html.erb

or

git checkout -- *

removes all changes made to unstaged files in git status eg

modified:    app/controllers/posts.rb
modified:    app/views/posts/index.html.erb
Zarne Dravitzki
  • 1,648
  • 1
  • 16
  • 15
  • 8
    `git checkout -- *` doesn't work for me unless I'm in the directory where the changed files are located. To checkout all files across the whole repository, you must do `git checkout -- :/` – waldyrious Apr 04 '16 at 19:02
  • In `git checkout -- *`, the star is replaced by the Shell, with all files and directories in the current directory. So it should go in subdirectories. It works for me. But thanks to highlight the syntax ":/" that seams cleaner in my opinion. – mcoolive Nov 30 '16 at 12:40
  • It seems that I can `git checkout -- '**/*.md'` as well. Just as what I need right now. – Polv Jun 19 '20 at 02:15
66

One non-trivial way is to run these two commands:

  1. git stash This will move your changes to the stash, bringing you back to the state of HEAD
  2. git stash drop This will delete the latest stash created in the last command.
mrtimuk
  • 53
  • 9
glumgold
  • 789
  • 5
  • 6
32

Git 2.23 introduced the git restore command to restore working tree files.

To restore all files in the current directory:

git restore .

If you want to restore all C source files to match the version in the index, you can do

git restore '*.c'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
26
git clean -fd

didn't help, and new files remained. I totally deleted all the working tree and then

git reset --hard

See "https://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git/673420#673420" for advice to add the -x option to clean:

git clean -fdx

Note -x flag will remove all files ignored by Git, so be careful (see the discussion in the answer I refer to).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fr0sT
  • 2,959
  • 2
  • 25
  • 18
18

If you have an uncommitted change (it’s only in your working copy) that you wish to revert to the copy in your latest commit, do the following:

git checkout filename
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kgandroid
  • 5,507
  • 5
  • 39
  • 69
  • I'm trying this after an uncommitted `git rm filename`, and it's not working. `error: pathspec 'filename' did not match any file(s) known to git.` – falsePockets Dec 09 '19 at 22:18
  • The solution for undoing `git rm` is `git checkout master -- filename` – falsePockets Dec 09 '19 at 22:19
  • It works (tried with Git 2.17.1). But why is it different from [Zarne Dravitzki's answer](https://stackoverflow.com/questions/5807137/how-can-i-revert-uncommitted-changes-including-files-and-folders/15375309#15375309)? – Peter Mortensen May 14 '22 at 22:17
18

I think you can use the following command: git reset --hard

Malloc
  • 15,434
  • 34
  • 105
  • 192
Josnidhin
  • 12,469
  • 9
  • 42
  • 61
  • hhmm... I did that but my files are still there. Should I do something after ? – MEM Apr 27 '11 at 16:22
  • 2
    git reset only reverts the uncommited changes in the working tree. It will not remove the new files and folders. I am not sure how to do that with git – Josnidhin Apr 27 '11 at 16:38
  • 1
    So, if we change a system directory by adding new files and folders, and then we want to revert that directory to a previous state (w/out those files and folders), we cannot do that with git ? So the best we can is to revert file states ? But once we create a file, we can't remove that file unless we do it manually ? – MEM Apr 27 '11 at 17:37
10

Please note that there might still be files that won't seem to disappear - they might be unedited, but Git might have marked them as being edited because of CRLF / LF changes. See if you've made some changes in .gitattributes recently.

In my case, I've added CRLF settings into the .gitattributes file and all the files remained in the "modified files" list because of this. Changing the .gitattributes settings made them disappear.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob
  • 4,927
  • 4
  • 26
  • 41
  • me too. Following an attempt to renormalize my line endings, I had an error in my gitattributes, which made "git reset --hard" only unstage and not undo the modifications – timB33 Feb 09 '23 at 11:35
7

You can just use following Git command which can revert back all the uncommitted changes made in your repository:

git checkout .

Example:

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   application/controllers/Drivers.php
        modified:   application/views/drivers/add.php
        modified:   application/views/drivers/load_driver_info.php
        modified:   uploads/drivers/drivers.xlsx

no changes added to commit (use "git add" and/or "git commit -a")

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git checkout .

ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Haritsinh Gohil
  • 5,818
  • 48
  • 50
6

From Git help:

 Changes to be committed:
      (use "git restore --staged <file>..." to unstage)

    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
JDValle
  • 83
  • 2
  • 8
-2

I usually use this way that works well:

mv fold/file /tmp
git checkout fold/file
thinkhy
  • 923
  • 13
  • 25
  • 1
    This is exactly the same what the guy with 357 "likes" proposed. Only that you even do create a backup of the newly checked out file. – Matthias May 11 '17 at 07:29
-3

A safe and long way:

  1. git branch todelete
  2. git checkout todelete
  3. git add .
  4. git commit -m "I did a bad thing, sorry"
  5. git checkout develop
  6. git branch -D todelete
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
-3

Use:

git reset HEAD filepath

For example:

git reset HEAD om211/src/META-INF/persistence.xml
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Aniket
  • 173
  • 12