1396

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?

3
  • 1
    @Joel & Wayfarer: why don't you try git help reset and git help clean Commented Jan 4, 2015 at 16:53
  • 3
    Making an alias for this in your git config is a good idea ;) Commented Sep 9, 2015 at 17:02
  • 2
    I prefer to go with git stash (saves modified files on a stack) followed by git stash drop (deletes it). Commented Jul 26, 2022 at 10:40

15 Answers 15

2015
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

Sign up to request clarification or add additional context in comments.

16 Comments

Note: git reset --hard removes staged changes as well as working directory changes. Also, git clean -f -d is probably a better opposite of adding a new untracked file. From the question, the asker may be quite happy with his current set of ignored files.
Read the next answer and watch out of the -x switch. (It might also remove your local config such as password/db-settings files. e.g. database.yml)
That -x switch is unnecessary and somewhat dangerous in this case.
git clean -fxd can actually be REALLY dangerous if you don't know what you're doing. You may end up permanently deleting some very important untracked files, such as your database, etc. Use caution.
Note that git clean -f -d will delete files from ignored folders too. So all you local logs and things like that will be gone. Usually it's not a big problem, but it's better to know.
|
437

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

6 Comments

I always git clean -nd . before actually deleting files using git clean -fd .
Why? Can you explain the detail please.
Per git clean -n option is actually a dry run which doesn't remove anything, it just shows you what will be done.
@tbear, You can always do a blank add -A + commit -a + revert head first before git clean. Reviewing each and every delete simply doesn't scale for major scenarios. Also, dry running is not a silver bullet: what if you missed something or made a mistake during the review?
This removes files, but it does not undo modifications.
|
260

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.

8 Comments

@Vincent: The -- avoids typing errors by telling the checkout command that no more parameters are specified. Without them you could end with a new branch instead of reseting the current one!
This is the safest route, but it will not remove untracked files (newly-added files).
This does not restore deleted, yet uncommitted, files.
Beginners might find it useful to know what that double dash (--) do there: Although git checkout file or git checkout . can revert all the changes made to a certain file(in the former one) or changes made to all files(in the latter one), but usually they're not used like that and a double dash precedes them in order to make it completely explicit that it's NOT going to be a "branch checkout". So even if a branch name and a file name are the same, there won't be any name conflict issues.
This didn't work for me. I have unstaged changes (not staged for commit) and yet git checkout -- . does nothing.
|
75

You can do this in two steps:

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

2 Comments

+1 because simple. Also because it works and does exactly what OP wants without scaring them off with omg this will delete everything.
Best solution. Does not scare people.
62

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.

1 Comment

I have tried git clean -i and chosen "clean" from menu - this finally removed new files.
50

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

6 Comments

Why are you referencing stash@{0} instead of just git stash drop?
Honestly, I don't remember :)
This one worked. As a note, ensure you are in the home directory ( git add -A . ). I have lost 30m becase there was no file match. Thanks!
I used to use this approach, and it has merit. However, I have since found the solutions proposed by jfountain and Heath Dutton more apt to the original problem, IMHO.
git stash drop stash@{0} drops latest stash if you did git stash it would drop all stashes you did
|
43

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.

1 Comment

This doesn't work, I still get a conflict warning when doing a git pull afterwards: CONFLICT (content): Merge conflict in... I only solved this by a git pull --strategy=ours
7

If you have pushed your changes to a remote branch and both branches point to the same HEAD, you can use:

git reset --hard origin/{branchName}

It will delete all untracked files.
Otherwise please look at other answers.

Comments

6

I think the safest and yet quick approach is with:

git stash -u  
git stash drop

The -u flag includes untracked files as questioner asked.
You can always check that with git status.
There is no need to add them to the staging area first.

Command git stash drop is equal to git stash drop stash@{0}.
Both only delete the last added changes on a stash list. You can verify that by git stash list.
At least in git version 2.35.1. More on git stash here.

Also, I believe that comments are for clarification of the question only, not for the answers @tiago-martins-peres, @SHernandez.

Comments

5

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.

Comments

4

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

1 Comment

This alias I like very much
3

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.

Comments

2

For a specific folder I used:

git checkout -- FolderToClean/*

1 Comment

That doesn't delete untracked files, as OP states.
1

Having tried most the methods suggested here over the past few years, my preferred way is simply:

git stash -u

Why?

  • It's one line and easy to remember
  • It removes all changes from the current working directory (not just some changes, like altered files only, which some other solutions do)
  • It keeps the removed changes in a stash, so the changes can be recovered if you change your mind
    • This is obviously a matter of personal preference. If you need to permanently remove changes so they're unrecoverable (e.g. for security purposes), this isn't the way to do it. By storing the discarded alterations in a stash, you can get them back later if you change your mind.
      • One small tip: I like to use the --message argument so I can easily distinguish between 'junk' stashes and any more important ones.
git stash -u --message "Junk: discarded changes relating to xyz"

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.