Git Clean Flags

Most of us interact with Git using the provided command line tools. The “git” command comes with a set of subcommands which denotes the operation that we wish to perform. For example: git commit, git pull, git push, and many more.

The “git clean” is part of that ecosystem and allows us to remove the untracked files and directories from the repository.

Untracked files in Git are basically files that are not currently managed by Git or in simple terms are not considered part of the repo in which you are working.

In this tutorial, we will explore the workings of the “git clean” command, how it works, the various flags, and example usages. This allows us to quickly and efficiently remove unnecessary files that might be cluttering your project with no real value in the codebase.

Command Syntax

Let us start with the basics and explore the syntax of the “git clean” command.

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] [<pathspec>...]

Let us explore what each flag does when used in the “git clean” command.

Understanding What Each Flag Does

-d or –directory

The “-d” flag allows to tell the “git clean” command to also remove the untracked directories in addition to the untracked files. By default, the command only removes the untracked files. Using the “-d” flag tells Git to include directories as well.


git clean -d

-f or –force

The “-f” or “–force” option tells the “git clean” command to force the removal of untracked files and directories. If Git thinks that the untracked files and directories might be important to the functionality, Git will fail to remove them.

You can then force it to remove them using the “-f” option. It is good to keep in mind that this is a destructive option and will lead to data loss.

-i or –interactive

The “-i” option tells Git to run the “clean” command in interactive mode. This manually prompts you to confirm or reject the removal of a given file or directory.

If you want to selectively delete the untracked files, this option comes in hand and prevents you from accidentally removing important files.

-n or –dry-run

This flag allows us to simulate the “git clean” operation without actually removing any files or directories. It provides a list of files and directories that would be deleted if you ran the command without the “-n” flag.

Think of it as a way of previewing what will be deleted when you actually run the command.

-q or –quiet

The “quiet” option tells the “git clean” to be quiet. This means that the command will only show the errors instead of being verbose and showing a message for every removed file and directory.

-e or –exclude

The “-e” flag allows us to specify the patterns of files or directories to exclude from the “git clean” operation.

We can provide a path or pattern to exclude certain files or directories from being deleted as shown in the following example:

git clean -f -e "*.log"

This excludes any file that ends in the “.log” extension.


The “-x” option tells the command to use the standard ignore rules but still use the ignore rules provided in the “-e” option.


Lastly, the “-X” option tells the command to only remove the files that are ignored by Git. This is useful to rebuild everything from scratch but keep the manually created files.


In this tutorial, we learned about the basics of the “git clean” command to remove the untracked files and directories from a Git repo. We also explored the provided flags to modify the working of the command to our suitable needs.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list