git: undo all working dir changes including new files

Git

Git Problem Overview


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 Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

You can do this in two steps:

  1. Revert modified files: git checkout -f
  2. Remove untracked files: git clean -fd

Solution 6 - Git

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

Solution 7 - Git

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.

Solution 8 - Git

git reset --hard origin/{branchName}

It will delete all untracked files.

Solution 9 - Git

git clean -i will first show you the items to be deleted and proceed after your confirmation. I find this useful when dealing with important files that should not be deleted accidentally.

See git help clean for more information, including some other useful options.

Solution 10 - Git

If you want to discard all changes, you can use any of the valid options in an alias in .gitconfig. For instance:

[alias]
    discard = "!f() { git add . && git stash && git stash drop stash@{0}; }; f"

Usage: git discard

Solution 11 - Git

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.

Solution 12 - Git

For a specific folder I used:

git checkout -- FolderToClean/*

Solution 13 - Git

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:

  1. bring up a context menu for the needed folder and select revert, it shows a revert popup where you can select changed files to revert/recover.
  2. If you also want to delete added files(that are not in git yet) click commit (from same context menu) it brings up the Commit popup and shows you the added files, then right click each of them and choose delete. But dont press Commit btn in this popup, as you dont wanna commit, but only see added files and delete them from here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlerView Question on Stackoverflow
Solution 1 - GitrdtscView Answer on Stackoverflow
Solution 2 - GitHeath DuttonView Answer on Stackoverflow
Solution 3 - GitjfountainView Answer on Stackoverflow
Solution 4 - GitGreg HewgillView Answer on Stackoverflow
Solution 5 - GitGerard de VisserView Answer on Stackoverflow
Solution 6 - GitdonquixoteView Answer on Stackoverflow
Solution 7 - GitskubeView Answer on Stackoverflow
Solution 8 - Gitsantosh kumarView Answer on Stackoverflow
Solution 9 - Gittg_soView Answer on Stackoverflow
Solution 10 - GitAgorrecaView Answer on Stackoverflow
Solution 11 - GitdonquixoteView Answer on Stackoverflow
Solution 12 - GitGlauco NevesView Answer on Stackoverflow
Solution 13 - GitOleg VorontsovView Answer on Stackoverflow