git-clean - Remove untracked files from the working tree

Synopsis

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…​

Description

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.

If any optional <path>... arguments are given, only those paths are affected.


Step 1 is to show what will be deleted by using the -n option:

# Print out the list of files and directories which will be removed (dry run)
git clean -n -d

Clean Step - beware: this will delete files:

# Delete the files from the repository
git clean -f
  • To remove directories, run git clean -f -d or git clean -fd
  • To remove ignored files, run git clean -f -X or git clean -fX
  • To remove ignored and non-ignored files, run git clean -f -x or git clean -fx

Note the case difference on the X for the two latter commands.

If clean.requireForce is set to "true" (the default) in your configuration, one needs to specify -f otherwise nothing will actually happen.

Again see the git-clean docs for more information.


Options

-f, --force

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f, -n or -i.

-x

Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.

-X

Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

-n, --dry-run

Don’t actually remove anything, just show what would be done.

-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.

Top answer
1 of 16
10109

git-clean - Remove untracked files from the working tree

Synopsis

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…​

Description

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.

If any optional <path>... arguments are given, only those paths are affected.


Step 1 is to show what will be deleted by using the -n option:

# Print out the list of files and directories which will be removed (dry run)
git clean -n -d

Clean Step - beware: this will delete files:

# Delete the files from the repository
git clean -f
  • To remove directories, run git clean -f -d or git clean -fd
  • To remove ignored files, run git clean -f -X or git clean -fX
  • To remove ignored and non-ignored files, run git clean -f -x or git clean -fx

Note the case difference on the X for the two latter commands.

If clean.requireForce is set to "true" (the default) in your configuration, one needs to specify -f otherwise nothing will actually happen.

Again see the git-clean docs for more information.


Options

-f, --force

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f, -n or -i.

-x

Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.

-X

Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

-n, --dry-run

Don’t actually remove anything, just show what would be done.

-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.

2 of 16
1335

Use git clean -f -d to make sure that directories are also removed.

  1. Don’t actually remove anything, just show what would be done.

    git clean -n
    

    or

    git clean --dry-run
    
  2. 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 the -f option twice if you really want to remove such a directory.

    git clean -fd
    

You can then check if your files are really gone with git status.

Discussions

git checkout force deletes tracked directory containing untracked files
Under certain conditions a forced checkout will delete untracked files by default. Reproduction steps Initialize a test git repo with one commit and a directory with a tracked file and an untracked file mkdir test-repo cd test-repo git i... More on github.com
🌐 github.com
0
February 15, 2017
Reset and Checkout differ from git's handling of untracked files
Porcelain operations Reset and Checkout delete untracked files in situations where standard git does not. Repro case: Test setup: git clone https://github.com/src-d/git-fixture cd git-fixture echo "foo" > bar # ./bar is untracked git beh... More on github.com
🌐 github.com
1
November 19, 2018
Lost untracked files
That situation is kind of outside the scope of what/how git is used. If you tell git to ignore files, it will. Of course the lost directory will be gone from all branches because it isn’t part of any branch, it was ignored. By git init, you are giving git control of the directory. Any important files you need to keep should either be version controlled, or stored outside of the git repo. You’ll need to recover from a backup (you DO have regular backups right?), not with any git commands. Some git commands wipe the working directory to a clean slate (removing any untracked files, etc). More on reddit.com
🌐 r/git
7
2
February 26, 2024
"The following untracked working tree files would be overwritten by checkout"
Here is a sequence for illustration: $ mkdir untracked $ cd untracked $ git init $ date > DATE $ git add DATE $ git commit -m "Adding DATE file" $ date > TIME $ git checkout -b has-time $ git add TIME $ git commit -m "Adding TIME file" $ git checkout - # go back to original branch (main) $ ls TIME Result: No such file, because it is not in the original (main) branch $ date > TIME $ git checkout has-time Result: error: The following untracked working tree files would be overwritten by checkout: TIME Please move or remove them before you switch branches. Aborting More on reddit.com
🌐 r/git
10
2
August 2, 2024
🌐
LabEx
labex.io › tutorials › git-how-to-handle-error-untracked-working-tree-files-would-be-overwritten-by-checkout-in-git-417551
How to handle 'error: untracked working tree files would be overwritten by checkout' in Git | LabEx
Sometimes, the untracked file is not disposable; it's work you want to keep. In this case, the correct approach is to add the file to Git's tracking system by committing it. This section also covers best practices to prevent this error from happening in the first place. First, let's return to the main branch and recreate our conflicting file. cd ~/project/git-checkout-demo git checkout main echo "## My local changes to feature documentation" > feature.md
🌐
GitHub
github.com › libgit2 › libgit2 › issues › 4125
git checkout force deletes tracked directory containing untracked files · Issue #4125 · libgit2/libgit2
February 15, 2017 - mkdir test-repo cd test-repo git init echo committed > committed git add committed git commit -m "Initial commit" mkdir tracked-dir echo "tracked" > tracked-dir/tracked-file echo "untracked" > tracked-dir/untracked-file git add tracked-dir/tracked-file ... #include <git2.h> int main(int argc, char **argv) { git_libgit2_init(); git_repository *repo = NULL; git_repository_open(&repo, "./test-repo"); git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; opts.checkout_strategy = GIT_CHECKOUT_FORCE; git_checkout_head(repo, &opts); return 0; }
Author   libgit2
🌐
GitHub
gist.github.com › subfuzion › 1128192
Git Tips: Reset, Clean - Remove untracked files and directories from the working tree when switching branches or checking out different commits. · GitHub
... @Young-Je, git checkout will leave untracked files alone, they will be shown as 'untrack' in your new branch, if you run git clean -df, these untracked files/folder will be DELETED from your file system!!
🌐
GitHub
github.com › src-d › go-git › issues › 1026
Reset and Checkout differ from git's handling of untracked files · Issue #1026 · src-d/go-git
November 19, 2018 - Porcelain operations Reset and Checkout delete untracked files in situations where standard git does not. Repro case: Test setup: git clone https://github.com/src-d/git-fixture cd git-fixture echo "foo" > bar # ./bar is untracked git beh...
Author   src-d
🌐
CloudBees
cloudbees.com › blog › git-remove-untracked-files
How to Properly Remove Untracked Files With Git
October 5, 2021 - Git can’t recover it since it didn’t store it in the first place. When it comes to untracked files, the typical course of action would be to track them—using the command git add—so you can commit them, saving them inside your repo’s history.
Find elsewhere
🌐
GitHub
gist.github.com › aguilarcarlos › dc8ff22845991c860e67
Fixing untracked files · GitHub
Run the following commands from the top folder of your git repo. Note: Commit your current changes, or you will lose them. git rm -r --cached . git add --all git commit -m "Fixed untracked files" git push [origin] [branch]
🌐
Reddit
reddit.com › r/git › lost untracked files
r/git on Reddit: Lost untracked files
February 26, 2024 -

I believe I have lost forever a folder with some data that I had gitignored. This folder had never been tracked by git, so there is no way of retrieving it I believe.

This folder disappeared after a branch checkout, and is now gone from all branches. Looking for a solution I found that having multiple different .gitignore files could cause this issue, but all .gitignore files are the same in every branch as I have not modified it in some time.

I believe this is not the intended behavior of git, what could I have done wrong to lose this folder? Is there a way to protect gitignored files from being deleted by git?

Thanks in advance.

🌐
DataCamp
datacamp.com › tutorial › git-clean
Git Clean: Remove Untracked Files and Keep Repos Tidy | DataCamp
March 7, 2025 - However, the git clean command should be used with caution since it permanently deletes untracked files and directories without moving them to a trash or recycle bin. Unlike git reset or git checkout, which modify tracked files, git clean strictly deals with files and directories that are not ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-use-the-git-clean-command
git clean: How to remove untracked files in Git
Developers can use the git clean command to delete untracked files in a working tree and remove what they don't need from a local repository. Here is how to issue the command and some helpful tips ...
🌐
Reddit
reddit.com › r/webdev › how to git checkout without bringing new files to other branch?
r/webdev on Reddit: How to git checkout without bringing new files to other branch?
November 15, 2022 -

Context of the problem:
I created a new feature branch (breaking change feature) and created bunch of files in it (not ready for commit yet).
Now I have need to go back to my main and do some not related hotfix.
But when I checkout to my main branch all new files from feature branch are still there, and because its a breaking change I can't run npm run dev without tons of errors.
I want to see my main branch as it is in my github repo without bringing any changes from.

Commands I've used:
git stash // on feature branch
git checkout main // on feature branch
npm run dev // results in errors, due to breaking changes in new files

Research I have completed prior to requesting assistance:
Googled the title of this post.
Looked at stack overflow solutions telling to use git stash / git switch, but that didn't help.

----------

Solved.

Changed branches via GitHub Desktop app leaving all changes on feature branch.Why terminal git commands didn't work for me - I have no idea.

🌐
Career Karma
careerkarma.com › blog › git › removing untracked files with git
Removing Untracked Files with Git | Career Karma
December 29, 2020 - Untracked files are the opposite, those files were not in the previous commit and have not been staged to be committed. You have the option of either stage them and commit them to your repository, or remove them! If we do git status right after modifying/added files it would show us the list of untracked files and files that are tracked.
🌐
Bas Wijdenes
baswijdenes.com › home › microsoft › fix: the following untracked working tree files would be overwritten by checkout git
FIX: The following untracked working tree files would be overwritten by checkout GIT | Bas Wijdenes
March 7, 2025 - ... If you don’t care about the untracked files and the above didn’t work, we can also force check out. ... You don’t need the files → Delete them and try again. You just want to switch branches → Force checkout with -f.
🌐
GitHub
github.com › github › VisualStudio › issues › 1271
Can't checkout PR when there are untracked files · Issue #1271 · github/VisualStudio
October 17, 2017 - ... It uses IsDirty, which checks for any differences between the working directory/index and the last comment. By default, command line Git will allow a checkout if there are untracked files in the working directory.
Author   github
🌐
Career Karma
careerkarma.com › blog › git › git error: untracked files would be overwritten by checkout
Git Error: untracked files would be overwritten by checkout
October 15, 2020 - This error occurs when you have files that are on the current branch that have changes on the branch you are working on as well. The fix is fairly simple: do exactly what the last statement says in the error.
🌐
Git
git-scm.com › docs › git-checkout
Git - git-checkout Documentation
This is used to throw away local changes and any untracked files or directories that are in the way.