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.

🌐
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
Discussions

git checkout -- * and skip untracked files - Stack Overflow
I'm trying to restore files after a colossal screw up on my part. git checkout -- * is working great so far. Except that, for example, in my .gitignore file I have "LICENSE.txt" listed, and it More on stackoverflow.com
🌐 stackoverflow.com
December 11, 2015
How can I checkout an untracked file in a git stash? - Stack Overflow
Assume I have stashed some changes using one of: git stash -u git stash --include-untracked I can checkout an individual file from the stash via git checkout stash@{0} -- filename That works if git More on stackoverflow.com
🌐 stackoverflow.com
version control - Why does git checkout not delete new files? - Stack Overflow
I thought git checkout basically overwrote your current working files with the snapshot at the commit you give it, so I would have thought this would delete file.txt. But it doesn't. Why? ... Well, because your assumption was wrong. git checkout does not affect untracked files. More on stackoverflow.com
🌐 stackoverflow.com
Checkout removes untracked files
In Git, running checkout does not affect untracked files. Meanwhile go-git does the equivalent of a reset. All untracked files are lost. Is this intentional? More importantly, is there a workaround for this? More on github.com
🌐 github.com
7
December 15, 2023
🌐
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.
🌐
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.
🌐
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!!
Find elsewhere
🌐
GitHub
github.com › go-git › go-git › issues › 970
Checkout removes untracked files · Issue #970 · go-git/go-git
December 15, 2023 - In Git, running checkout does not affect untracked files. Meanwhile go-git does the equivalent of a reset. All untracked files are lost. Is this intentional? More importantly, is there a workaround for this?
Author   go-git
🌐
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 ...
🌐
Git
git-scm.com › docs › git-clean
Git - git-clean Documentation
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 <pathspec>... arguments are given, only those paths that match the pathspec are affected. ... Normally, when no <pathspec> is specified, git clean will not recurse into untracked directories to avoid removing too much.
🌐
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.

Top answer
1 of 2
3

When you switch to a branch, Git makes sure that your working tree matches the content of the commit you're switching. Quoting an extract of Basic Branching of the Git Pro book:

when you switch branches, Git resets your working directory to look like it did the last time you committed on that branch. It adds, removes, and modifies files automatically to make sure your working copy is what the branch looked like on your last commit to it.

This means that:

  • Every tracked file in your working directory that is not present in the selected commit will be removed.

  • Every tracked file in the selected commit that is not present in your working directory will be added.

  • Every tracked file that is present in both your working directory and the selected commit will be updated to the version contained in the commit.

In your case, even though master had a .gitignore for some of the files on gitignoreless, once you switched to master those files had to be removed in order to match the content of master's head commit. This is because the files were already tracked on gitignoreless, and every tracked file in the working directory that is not contained in the selected commit is removed.

As already explained by @matt, .gitignore affects only files that are not already in the index. Therefore, you might want to consider having your .gitignore on all your branches, and build it in such a way to not show undesired files during the staging process. However, since some of the undesired files have already been tracked, you can "untrack" them with the --cached option of git rm. In your case, you could run:

# untrack the undesired files
git rm --cached <file1> <file2> ... <fileN>

# add the previous files as entries in your gitignore
echo <file1> >> .gitgnore
echo <file2> >> .gitgnore
...
echo <fileN> >> .gitgnore
2 of 2
2

The entire approach here (indeed, the very question title itself) seems to be based on a misapprehension, or a cascade of misapprehensions, about gitignore (and perhaps Git itself):

  • gitignore is not branch-based.

  • gitignore does not affect what happens during checkout.

  • gitignore has no effect on files that already exist in the repo (i.e. in the current HEAD); it affects only files that are not already in the index, and it affects only what happens when you say things like git status or git add.

So, if the goal is simply to develop a gitignore, then just develop it, i.e. change it incrementally, over time, as experience suggests.

How? Well, as I said, this is all about what git status means (and what git add . means). So think about your gitignore as you are making a commit by assembling files in the index. Every time you are about to make a commit, say git status to see what Git "knows" about the working tree in relation to the index (and the index in relation to the HEAD).

  • If you see a file being tracked in a way you don't like (e.g. it is listed as untracked and you want it to be both untracked and not mentioned any longer), list it in gitignore.

  • If you see a file has already been made part of the index and it shouldn't be, remove it from the index and list it in gitignore.

  • If files that you don't think should be checked in have already been checked in (i.e. not just in the index but in the HEAD), gitignore will have no effect on that. It would then be incumbent on you to delete such a file from the index and check that in as a commit, so that the file doesn't reappear in the index every time you switch to this branch.

Ultimately, though, don't expect gitignore to work some kind of magic. It is merely a time-saver, to help keep files from being checked into the repo that you do not want shared between developers (or with the public, if it's a public repo). Use it for what it's intended for and you'll be fine.

🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-remove-local-untracked-files-from-current-git-working-tree
Remove Local Untracked Files From Current Git Working Tree - GeeksforGeeks
May 7, 2026 - By running the git clean command without any flags, a fatal error is generated and explicitly asks us to mention a flag ... To have a look at all the untracked files that will be removed, we can use the git clean command with the -n flag.
🌐
w3tutorials
w3tutorials.net › blog › cannot-checkout-branch-or-remove-problematic-files
Git Error: Cannot Checkout Branch Due to Untracked Files? How to Remove Problematic Files Without Switching Branches — w3tutorials.net
This common Git hiccup occurs when untracked files in your current working directory conflict with files in the branch you’re trying to checkout. Git prevents overwriting these untracked files to avoid accidental data loss, leaving you stuck—unable to switch branches until the conflict ...
🌐
LinkedIn
linkedin.com › pulse › i-lose-untracked-files-when-changing-git-branches-paul-christie
Will I lose Untracked Files when changing git branches?
March 9, 2018 - What happens when you checkout other branches, are these staged and unchecked files deleted? Let's see.. Assuming we have created our branch from an empty master repo, subsequently 3 files have been commited to master (masterfile1, masterfile2, masterfile3). We then switch to 'branch' and create 3 new files, 'add/commit' one and 'add' another. We now have 3 files in our branch working folder, branchfile1 is committed, branchfile2 is staged, branchfile3 is untracked.
🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Recording-Changes-to-the-Repository
2.2 Git Basics - Recording Changes to the Repository
The git rm command does that, and also removes the file from your working directory so you don’t see it as an untracked file the next time around. If you simply remove the file from your working directory, it shows up under the “Changes not staged for commit” (that is, unstaged) area of your git status output: $ rm PROJECTS.md $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: PROJECTS.md no changes added to commit (use "git add" and/or "git commit -a")