How can I undo every change made to my directory after the last commit, including deleting added files, resetting modified files, and adding back deleted files?
-
possible duplicate of [Revert to previous Git commit](http://stackoverflow.com/questions/4114095/revert-to-previous-git-commit) – nawfal Feb 09 '14 at 18:45
-
8@nawfal may be a duplicate, but 'reset all changes after last commit' matches more searching criteria(words searched in google) than corresponding 'how to revert git repository'. At least for people like me who don't have English as their mother tongue :d – Shirish Herwade Aug 04 '17 at 07:33
3 Answers
First, reset any changes
This will undo any changes you've made to tracked files and restore deleted files:
git reset HEAD --hard
Second, remove new files
This will delete any new files that were added since the last commit:
git clean -fd
Files that are not tracked due to .gitignore
are preserved; they will not be removed
Warning: using -x
instead of -fd
would delete ignored files. You probably don't want to do this.

- 3,318
- 1
- 14
- 24

- 55,163
- 11
- 60
- 80
-
9@Adam: You may sometimes want the `-x` option to `git clean` as well, which directs it to remove ignored files as well. – Cascabel Jan 08 '11 at 00:35
-
39If you want to keep files that are not tracked due to .gitignore, be careful with the `git clean -fd` command. – bitsoflogic Sep 23 '14 at 13:12
-
4@Levinaris: It is the other way around `git clean -fd` will not delete ignored files. `-x` will. – Robert Siemer Jan 13 '15 at 16:14
-
7@RobertSiemer Actually, it can! If you have any folders composed entirely of ignored files it will remove those folders thus deleting ignored files. Consider a .gitignore file like the one here: http://stackoverflow.com/q/25554504/456645. In this example, assume some folders have no PHP files. `git clean -fd` will delete those folders and untracked files. Tested with git version 1.9.1 – bitsoflogic Jan 20 '15 at 17:24
-
-
2
-
1Tried the above command and my entire hard disk contents got deleted. Im unable to restore it anymore. Is there any ways to restore it back? – Sep 09 '16 at 11:29
-
-
-
4
-
2I'm not upvoting this answer because it is on 444 and its look cool. – Siraj Alam Sep 21 '18 at 07:10
-
-
@Learner am about to run the above commands, was that a joke about it deleting the entire disk contents ? – steven7mwesigwa Jan 31 '19 at 21:42
How can I undo every change made to my directory after the last commit, including deleting added files, resetting modified files, and adding back deleted files?
You can undo changes to tracked files with:
git reset HEAD --hard
You can remove untracked files with:
git clean -f
You can remove untracked files and directories with:
git clean -fd
but you can't undo change to untracked files.
You can remove ignored and untracked files and directories
git clean -fdx
but you can't undo change to ignored files.
You can also set clean.requireForce
to false
:
git config --global --add clean.requireForce false
to avoid using -f
(--force
) when you use git clean
.

- 56,620
- 24
- 188
- 240
-
3Awesome, just what I needed. Thanks for the comparison of all the relevant commands! – Marquee Jan 25 '17 at 16:56
-
`git reset HEAD --hard` seemed to be enough for me to undo changes to the latest commit. – Pathros May 07 '22 at 21:53
There are two commands which will work in this situation,
root>git reset --hard HEAD~1
root>git push -f
For more git commands refer this page

- 445
- 4
- 14
-
6`git push -f` is not related to the question and, in this scenario, is dangerous – mustache1up Feb 11 '20 at 22:19
-
2This will remove the last commit from the remote repository, instead of dropping all changes *since* the last commit. – TheKodeToad Nov 26 '21 at 10:43
-
1Please delete this answer, it does the opposite of what the OP asked, and thus unsafe. Deleting this answer will also restore your lost rep. – aggregate1166877 Sep 21 '22 at 03:18
-
1