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?
How can I revert uncommitted changes including files and folders?
996.3k views Asked by MEM AtThere are 14 answers
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).
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'
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
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.
You can run these two commands: