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.

🌐
Git
git-scm.com › docs › git-clean › 2.23.0
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 <path>... arguments are given, only those paths are affected. ... Remove untracked directories in addition to untracked files.
Discussions

Why doesn’t git reset --hard HEAD remove untracked files?
untracked.txt is "not in the way of writing any tracked files" therefore does not get touched. An example of where this would matter is if you had "untracked.txt" committed in an older commit, deleted it in a newer commit, recreated it in the working tree, and then reset to the older commit. If you want to purge out everything except what is held in tracked files, then you want Git - git-clean Documentation More on reddit.com
🌐 r/git
17
6
January 8, 2025
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
How to reset back to zero changes
I've tried git reset --hard but it does not delete files I've created. Right. git reset is a helpful tool, but it does a specific thing. With Git, your files are divided into two categories: tracked and untracked. Tracked files are that Git is keeping track of for you. When you make a Git commit, there's a snapshot of your entire tree of files, and they get included in this snapshot. And if you switch to an older or newer version, a different branch, etc., these files get updated to match. Untracked files are other files in your tree of files that you haven't asked Git to track. When you use git add to include file in a Git commit that you're in the process of creating, a side effect is that the file changes from untracked to tracked. If you never do this (and nobody else does either), then a file stays untracked. Untracked files are useful for stuff like temporary files (that you don't care about) or build output (that you can create by running build commands). As I'm sure you've guessed by now, git reset works on tracked files but leaves untracked files alone (with some very small exceptions). If you want to delete all untracked files, you can use git clean. In your case, you may want to use git clean -d to make it less conservative about what it deletes. Obviously, you want to be careful with git clean since its purpose is to delete stuff. In many cases, you might want to use both git reset and git clean if you really want a clean slate. One more thing: if you've created any commits and you want to get rid of those, you can also use git reset for that. Like a lot of Git commands, it can do a few different things which are only kinda related. It can reset your files to the latest commit on your current branch, or it can change what actually is the latest commit on your current branch. Whether you need to worry about that right now depends on whether you did any commits or not. More on reddit.com
🌐 r/git
2
1
October 31, 2023
[ Removed by moderator ]
I don’t really have an answer but definitely don’t initialize a git repo at the root level of your entire computer file system More on reddit.com
🌐 r/vscode
27
0
May 20, 2024
🌐
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
The -f flag forces the removal of untracked files. Directories and ignored files are left alone. The -d flag forces removal of untracked git directories.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git clean
How to Remove Untracked Files in Git? | Atlassian Git Tutorial
Git clean is a convenience method for deleting untracked files in a repo's working directory. Learn more about usage, examples, and interactive mode here.
🌐
Reddit
reddit.com › r/git › why doesn’t git reset --hard head remove untracked files?
r/git on Reddit: Why doesn’t git reset --hard HEAD remove untracked files?
January 8, 2025 -

Hi everyone,

I've been trying to fully understand how git reset --hard HEAD works, and I've run into a bit of a confusion. According to the man page for git reset, it says:

Resets the index and working tree. Any changes to tracked files in the working tree since `<commit>` are discarded. Any untracked files or directories in the way of writing any tracked files are simply deleted

From my understanding, the working tree includes both tracked and untracked files. However, when I run git reset --hard HEAD, only the tracked files are reset to their state in the commit, and untracked files remain untouched.

For example If I create a new untracked file (untracked.txt) and modify a tracked file (tracked.txt), running git reset --hard HEAD only resets tracked.txt, while untracked.txt stays there.

If the command is resetting the working tree, shouldn't it reset all files in the working tree, including untracked ones? Why does Git treat untracked files differently here?

Thanks!

🌐
Sentry
sentry.io › sentry answers › git › remove untracked files from the working tree in git
Remove untracked files from the working tree in Git | Sentry
June 15, 2023 - If we’re happy with the output of this command, we can run it again with the -f flag in place of -n: ... To remove untracked directories as well as untracked files, we can add the -d flag.
Find elsewhere
🌐
DataCamp
datacamp.com › tutorial › git-clean
Git Clean: Remove Untracked Files and Keep Repos Tidy | DataCamp
March 7, 2025 - The git clean command is a useful tool in Git that removes untracked files from the working directory. The git clean command is useful when you need to reset your working directory to a pristine state, such as before switching branches or after running build processes that generate temporary files.
🌐
CloudBees
cloudbees.com › blog › git-remove-untracked-files
How to Properly Remove Untracked Files With Git
October 5, 2021 - We’ll start by defining in more detail what untracked files are and why you’d want to get rid of them. After that, you’ll get your hands dirty learning how to do it in practice. If you read any of our Git tutorials, you know how it goes: We expect you to be familiar with some basic Git commands and be comfortable working with the command line.
🌐
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 - To remove the unstaged files and the unstaged directories, we have to use the git clean command with the -f and -d flags. ... To remove the untracked files and the files ignored by git, we have to use the git clean command with the -f flag and ...
🌐
30 Seconds of Code
30secondsofcode.org › home › git › branch › discard changes
Discard uncommitted or untracked changes in Git - 30 seconds of code
April 12, 2024 - ... Be careful when using git clean -f -d, as it is a potentially destructive action. Make sure you don't have any changes you want to keep before running this command. # Usage: git clean -f -d git clean -f -d # Discards all untracked changes
🌐
Better Stack
betterstack.com › community › questions › git-undo-all-working-dir-changes-including-new-files
Git: Undo All Working Dir Changes Including New Files | Better Stack Community
August 12, 2024 - Discard changes to tracked files with git checkout -- .. Remove untracked files and directories with git clean -fd.
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-delete-untracked-files-in-git
How to Delete Untracked Files in Git? - GeeksforGeeks
May 28, 2024 - If you also want to remove untracked directories, use the -d option along with -f. If you want to remove files that are ignored by Git (specified in the .gitignore file), use the -x option:
🌐
Graphite
graphite.com › guides › git-ignore-untracked-files
Git ignore untracked files
Create or modify the .gitignore in your project's root directory and add patterns for the files or directories you want to ignore. This setup prevents these files from being shown as untracked and keeps them out of your repository.
🌐
Oxford University Computing Laboratory
opsis.eci.ox.ac.uk › swc-git-novice › instructor › 06-ignore.html
Version Control with Git: Ignoring Things
September 18, 2023 - Once we have created this file, the output of git status is much cleaner: ... On branch main Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore nothing added to commit but untracked files present (use "git add" to track)
🌐
Hostinger
hostinger.com › home › tutorials › most used git commands from basic to advanced to simplify your workflow + free git cheat sheet
Git Commands for Repository Management + Free Cheat Sheet
February 24, 2025 - This command logs Git branch changes. It allows you to track your repository’s timeline, even when commits are deleted or lost: ... Last but not least, this command removes untracked files from your working directory, resulting in a clean and organized repository:
🌐
Ubuntu Manpages
manpages.ubuntu.com › focal › man(1)
Ubuntu Manpage: git-clean - Remove untracked files from the working tree
This can, for example, be useful to remove all build products. If any optional <path>... arguments are given, only those paths are affected. ... Normally, when no <path> is specified, git clean will not recurse into untracked directories to avoid removing too much.
🌐
The Great Magnet
greatmagnet.co.uk › tutorials › 2021 › 07 › 07 › untrack-ignored-files
Untracking ignored files with Git | The Great Magnet
July 7, 2021 - This will remove all files from the Git index (don’t worry, they haven’t gone anywhere).
🌐
Swansea
sa2c.swansea.ac.uk › git-demystified › 02-commits-revisited › index.html
Git: Beyond the Basics: Understanding Add and Commit
November 23, 2021 - On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: blue.txt modified: green.txt modified: red.txt Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: red.txt Untracked files: (use "git add <file>..." to include in what will be committed) other/