Description
The git
clean is an undo command, which completes other commands such as git reset and git checkout. However, unlike the other commands that operate on files already added to the Git tracking index, the git clean command runs on uncrawled files. Untracked files are those created within the working directory, but are not yet added to the tracking index. The following example shows the difference between tracked and untracked files:
As a result of the previous example, you will have a new Git repository in the test_directory directory, which then creates a test_tracked_file added to the Git index. In addition, a test_untracked_file and a test_untracked_dir are created. Next, the example calls the git state, which displays the output pointing to the internal state of tracked and untracked changes. The git clean command is executed to display its default target.
At this point, you should not run the git clean command. The following example shows what errors it can cause. Initially, Git is configured globally to require a “force” option to start git cleanup. Once executed, you cannot undo clean git. When fully executed, it will create a hard file system removal. You need to make sure that you really want to delete the untracked files before running it.
Common options and usage
The git clean command has several uses with different options
.
The -n option performs a git clean test. Displays the files to be deleted, but does not delete them.
As you can see, it says that the test_untracked_file will be deleted when the git clean command is executed.
The -force option is required unless the clean.requireForce configuration option is set to false. Deletes uncrawled files from the current directory, except for uncrawled folders or files specified with .gitignore.
The output shows that the test_untracked_file has been deleted. At this point, the git state will show that the test_untracked_file has been deleted and cannot be found. By default, git clean -f will work on all untracked files within the current directory. In addition, a <path> value can be passed with the -f option that will delete a specific file.
If you want to delete any uncrawled directories, you can use the -d option that tells git clean to do so, as it will ignore directories by default.
You can also use the -dn combination.
First, it generates that test_untracked_file that is ready for disposal. Then we execute a forced cleaning and receive output that is test_untracked_dir removed.
The -x option indicates git clean to also include ignored files. You’d better run a ‘dry run’ first, before the final deletion. The -x option will act on all ignored files. This could be unwanted things like ./.idea IDE configuration files.
The -x option can be passed and composed with other options. The example above is a combination with -f that will delete uncrawled files from the current directory, as well as any files that Git usually ignores.
Interactive
mode
The git clean has an interactive mode that is activated by passing the -ioption option. In the following example we also use the -d option, to act on the test_untracked_dir. After activating the interactive mode, it will display a message What > now. This message will prompt you to select a command to apply to untracked files. These commands are 6.
Let’s see through each command below.
Command
- 6 will explain the other commands
- Command 1 will delete the items mentioned
- Selecting command 2 will display an additional message to filter the list of untracked files. After choosing command 2, we will need the file wildcard pattern *_, which will restrict the list of untracked files to test_untracked_dir.
- Like command 2, command 3 is for refining the list of untracked file names. Selecting this command will prompt for numbers that match an untracked file name.
- Command 4 will run on each untracked file and display a Y/N message to confirm the deletion
- Selecting command 5 closes the interactive session.
.
.
.