Common options and usage
Given the previous explanation of the default
git clean behaviors and caveats, the following content demonstrates various
git clean use cases and the accompanying command line options required for their operation.
-n option will perform a “dry run” of
git clean. This will show you which files are going to be removed without actually removing them. It is a best practice to always first perform a dry run of
git clean. We can demonstrate this option in the demo repo we created earlier.
$ git clean -n Would remove untracked_file
The output tells us that
untracked_file will be removed when the
git clean command is executed. Notice that the
untracked_dir is not reported in the output here. By default
git clean will not operate recursively on directories. This is another safety mechanism to prevent accidental permanent deletion.
-f or --force
The force option initiates the actual deletion of untracked files from the current directory. Force is required unless the
clean.requireForce configuration option is set to false. This will not remove untracked folders or files specified by
.gitignore. Let us now execute a live
git clean in our example repo.
$ git clean -f Removing untracked_file
The command will output the files that are removed. You can see here that
untracked_file has been removed. Executing
git status at this point or doing a
ls will show that
untracked_file has been deleted and is nowhere to be found. By default
git clean -f will operate on all the current directory untracked files. Additionally, a
-f option that will remove a specific file.
git clean -f
-d include directories
-d option tells
git clean that you also want to remove any untracked directories, by default it will ignore directories. We can add the
-d option to our previous examples:
$ git clean -dn Would remove untracked_dir/ $ git clean -df Removing untracked_dir/
Here we have executed a 'dry run' using the
-dn combination which outputs
untracked_dir is up for removal. Then we execute a forced clean, and receive output that
untracked_dir is removed.
-x force removal of ignored files
A common software release pattern is to have a build or distribution directory that is not committed to the repositories tracking index. The build directory will contain ephemeral build artifacts that are generated from the committed source code. This build directory is usually added to the repositories
.gitignore file. It can be convenient to also clean this directory with other untracked files. The
-x option tells
git clean to also include any ignored files. As with previous
git clean invocations, it is a best practice to execute a 'dry run' first, before the final deletion. The
-x option will act on all ignored files, not just project build specific ones. This could be unintended things like ./.idea IDE configuration files.
git clean -xf
-x can be passed and composed with other options. This example demonstrates a combination with
-f that will remove untracked files from the current directory as well as any files that Git usually ignores.
Interactive mode or git clean interactive
In addition to the ad-hoc command line execution we have demonstrated so far,
git clean has an "interactive" mode that you can initiate by passing the
-i option. Let us revisit the example repo from the introduction of this document. In that initial state, we will start an interactive clean session.
$ git clean -di Would remove the following items: untracked_dir/ untracked_file *** Commands *** 1: clean 2: filter by pattern 3: select by numbers 4: ask each 5: quit 6: help What now>
We have initiated the interactive session with the
-d option so it will also act upon our
untracked_dir. The interactive mode will display a
What now> prompt that requests a command to apply to the untracked files. The commands themselves are fairly self explanatory. We'll take a brief look at each in a random order starting with command
6: help. Selecting command 6 will further explain the other commands:
What now> 6 clean - start cleaning filter by pattern - exclude items from deletion select by numbers - select items to be deleted by numbers ask each - confirm each deletion (like "rm -i") quit - stop cleaning help - this screen ? - help for prompt selection
Is straight forward and will exit the interactive session.
Will delete the indicated items. If we were to execute
1: clean at this point
untracked_dir/ untracked_file would be removed.
4: ask each
will iterate over each untracked file and display a
Y/N prompt for a deletion. It looks like the following:
*** Commands *** 1: clean 2: filter by pattern 3: select by numbers 4: ask each 5: quit 6: help What now> 4 Remove untracked_dir/ [y/N]? N Remove untracked_file [y/N]? N
2: filter by pattern
Will display an additional prompt that takes input used to filter the list of untracked files.
Would remove the following items: untracked_dir/ untracked_file *** Commands *** 1: clean 2: filter by pattern 3: select by numbers 4: ask each 5: quit 6: help What now> 2 untracked_dir/ untracked_file Input ignore patterns>> *_file untracked_dir/
Here we input the
*_file wildcard pattern which then restricts the untracked file list to just
3: select by numbers
Similar to command 2, command 3 works to refine the list of untracked file names. The interactive session will prompt for numbers that correspond to an untracked file name.
Would remove the following items: untracked_dir/ untracked_file *** Commands *** 1: clean 2: filter by pattern 3: select by numbers 4: ask each 5: quit 6: help What now> 3 1: untracked_dir/ 2: untracked_file Select items to delete>> 2 1: untracked_dir/ * 2: untracked_file Select items to delete>> Would remove the following item: untracked_file *** Commands *** 1: clean 2: filter by pattern 3: select by numbers 4: ask each 5: quit 6: help