Skip to navigation
Remove all untracked files from a git repository
07.05.26
``` git clean -fdx ``` Here's what each part of the command means: * **`git clean`**: This is the primary command. Its purpose is to remove untracked files from your working directory. Untracked files are files that Git is aware of but hasn't been told to track yet (they haven't been added to the staging area and committed). * **`-f`** (or `--force`): This option is **mandatory** for `git clean`. Git requires you to explicitly force the removal of untracked files. This is a safety mechanism to prevent accidental deletion of files you might want to keep. If you forget `-f`, Git will warn you and refuse to proceed. * **`-d`** (or `--directories`): This option tells `git clean` to also remove **untracked directories**. Without `-d`, `git clean` will only remove untracked files and will ignore untracked directories. * **`-x`** (or `--exclude-standard`): This option tells `git clean` to also remove files that are ignored by Git. Typically, `git clean` only operates on files that are *not* ignored (as defined in your `.gitignore` file). The `-x` flag overrides this behavior and includes ignored files in the cleanup process. **In summary, `git clean -fdx` will:** 1. **Remove all untracked files** in your current directory and its subdirectories. 2. **Remove all untracked directories** in your current directory and its subdirectories. 3. **Remove all ignored files and directories** as well. **When would you use `git clean -fdx`?** This command is most useful in a few scenarios: * **After a failed build or merge**: Sometimes, build processes or merge operations can leave behind temporary or leftover files that are no longer needed and clutter your working directory. * **To reset to a clean state**: If you want to completely wipe out everything that isn't tracked by Git (including build artifacts, temporary files, and even ignored configuration files), `git clean -fdx` is the way to go. * **When working with specific workflows**: Some development workflows or CI/CD pipelines might generate a lot of temporary files that you want to ensure are removed before the next step. **Important Considerations and Warnings:** * **This command is destructive and irreversible.** Once you run `git clean -fdx`, the files and directories are gone. They are not moved to the trash; they are permanently deleted. * **Always use `git clean -n` first!** Before running `git clean -fdx`, it's highly recommended to run `git clean -fdx -n` (or `git clean -nx`, `git clean -dn`, etc., depending on the flags you intend to use). The `-n` (or `--dry-run`) option will show you exactly which files and directories *would be* removed without actually deleting them. This is your chance to review and ensure you're not deleting anything important. * **Be in the correct directory**: Make sure you are in the root of your Git repository (or the specific directory you want to clean) before executing the command. * **Understand your `.gitignore`**: If you're using `-x`, be absolutely sure you understand what files are being ignored and if you truly want to delete them. **Example Scenario:** Let's say you have a project with the following files: * `README.md` (tracked) * `src/main.c` (tracked) * `build/` (untracked directory) * `build/app.o` (untracked file within the build directory) * `temp.log` (untracked file) * `config.local.yml` (ignored by `.gitignore`) If you run `git status`, you'll see `build/`, `build/app.o`, and `temp.log` as untracked. `config.local.yml` won't appear in `git status` but is in your `.gitignore`. 1. **`git clean -n`**: This would show you that `temp.log` would be removed. 2. **`git clean -dn`**: This would show you that `temp.log` and `build/` would be removed. 3. **`git clean -dxn`**: This would show you that `temp.log`, `build/` (and its contents like `build/app.o`), and `config.local.yml` would be removed. After confirming with the dry run, you could then proceed with `git clean -fdx` to actually perform the cleanup. Always exercise caution when using `git clean`, especially with the `-f` and `-x` flags!
Reply
Anonymous
Information Epoch 1778392452
Work your system no matter how imperfect, and modify it as you go.
Home
Notebook
Contact us