There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.
We can use the checkout command with the “–” option to remove all the changes that are not staged for commit: $ git checkout -- . We can see that our working directory now contains only the staged changes.
For all unstaged files in current working directory use: git restore . That together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation. If a file has both staged and unstaged changes, only the unstaged changes shown in git diff are reverted.
If you have committed changes to a file (i.e. you have run both git add and git commit ), and want to undo those changes, then you can use git reset HEAD~ to undo your commit.
git reset --hard # removes staged and working directory changes
## !! be very careful with these !!
## you may end up deleting what you don't want to
## read comments and manual.
git clean -f -d # remove untracked
git clean -f -x -d # CAUTION: as above but removes ignored files like config.
git clean -fxd :/ # CAUTION: as above, but cleans untracked and ignored files through the entire repo (without :/, the operation affects only the current directory)
To see what will be deleted before-hand, without actually deleting it, use the -n
flag (this is basically a test-run). When you are ready to actually delete, then remove the -n
flag:
git clean -nfd
Safest method, which I use frequently:
git clean -fd
Syntax explanation as per /docs/git-clean
page:
-f
(alias: --force
). If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete directories with .git sub directory or file unless a second -f is given.-d
. Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.As mentioned in the comments, it might be preferable to do a git clean -nd
which does a dry run and tells you what would be deleted before actually deleting it.
Link to git clean
doc page:
https://git-scm.com/docs/git-clean
For all tracked unstaged files use:
git checkout -- .
The .
at the end is important.
You can replace .
with a sub-directory name to clear only a specific sub-directory of your project. The problem is addressed specifically here.
Have a look at the git clean
command.
git-clean - Remove untracked files from the working tree
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
Normally, only files unknown to git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.
You can do this in two steps:
git checkout -f
git clean -fd
The following works:
git add -A .
git stash
git stash drop stash@{0}
Please note that this will discard both your unstaged and staged local changes. So you should commit anything you want to keep, before you run these commands.
A typical use case: You moved a lot of files or directories around, and then want to get back to the original state.
Credits: https://stackoverflow.com/a/52719/246724
I thought it was (warning: following will wipe out everything)
$ git reset --hard HEAD
$ git clean -fd
The reset
to undo changes. The clean
to remove any untracked files and directories.
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