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?
To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With