How to delete all changes from working directory including new untracked files. I know that git checkout -f
does that, but it doesn't delete new untracked files created since last commit.
Does anybody have an idea how to do that?
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.
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.
An alternative solution is to commit the changes, and then get rid of those commits. This does not have an immediate benefit at first, but it opens up the possibility to commit in chunks, and to create a git tag for backup.
You can do it on the current branch, like this:
git add (-A) .
git commit -m"DISCARD: Temporary local changes"
git tag archive/local-changes-2015-08-01 # optional
git revert HEAD
git reset HEAD^^
Or you can do it on detached HEAD. (assuming you start on BRANCHNAME branch):
git checkout --detach HEAD
git add (-A) .
git commit -m"DISCARD: Temporary local changes"
git tag archive/local-changes-2015-08-01 # optional
git checkout BRANCHNAME
However, what I usually do is to commit in chunks, then name some or all commits as "DISCARD: ...". Then use interactive rebase to remove the bad commits and keep the good ones.
git add -p # Add changes in chunks.
git commit -m"DISCARD: Some temporary changes for debugging"
git add -p # Add more stuff.
git commit -m"Docblock improvements"
git tag archive/local-changes-2015-08-01
git rebase -i (commit id) # rebase on the commit id before the changes.
# Remove the commits that say "DISCARD".
This is more verbose, but it allows to review exactly which changes you want to discard.
The git lol
and git lola
shortcuts have been very helpful with this workflow.
This is probably a noob answer, but: I use TortoiseGit for windows and it has a nice feature called REVERT. So what you do to revert your local nonstaged nonpushed changes is:
git help reset
andgit help clean
– SHernandez