How to revert uncommitted changes including files and folders?

GitGit CommitGit ResetGit RevertGit Clean

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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.

Solution 5 - Git

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).

Solution 6 - Git

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

Solution 7 - Git

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'

Solution 8 - Git

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

Solution 9 - Git

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.

Solution 10 - Git

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

Solution 11 - Git

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)

Solution 12 - Git

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

Solution 13 - Git

Use:

git reset HEAD filepath

For example:

git reset HEAD om211/src/META-INF/persistence.xml

Solution 14 - Git

I usually use this way that works well:

mv fold/file /tmp
git checkout fold/file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMEMView Question on Stackoverflow
Solution 1 - GithtanataView Answer on Stackoverflow
Solution 2 - GitRamashish BaranwalView Answer on Stackoverflow
Solution 3 - GitZarne DravitzkiView Answer on Stackoverflow
Solution 4 - GitglumgoldView Answer on Stackoverflow
Solution 5 - GitFr0sTView Answer on Stackoverflow
Solution 6 - GitJosnidhinView Answer on Stackoverflow
Solution 7 - GitTheKojuEffectView Answer on Stackoverflow
Solution 8 - GitkgandroidView Answer on Stackoverflow
Solution 9 - GitRobView Answer on Stackoverflow
Solution 10 - GitHaritsinh GohilView Answer on Stackoverflow
Solution 11 - GitJDValleView Answer on Stackoverflow
Solution 12 - GitJason LemayView Answer on Stackoverflow
Solution 13 - GitAniketView Answer on Stackoverflow
Solution 14 - GitthinkhyView Answer on Stackoverflow