How to use the command 'git clean' (with examples)
Git clean is a command used to remove untracked files from the working tree. It is useful when you want to get rid of files that are not being tracked by Git and are not needed in your project.
Use case 1: Delete files that are not tracked by Git
Code:
git clean
Motivation: When you want to remove all untracked files from your working tree, you can use this command. It will delete all files that are not being tracked by Git.
Explanation: The git clean
command without any options will delete all untracked files from the working tree. Untracked files are files that are not being tracked by Git and are not in the staging area.
Example output:
Removing untracked file test.txt
Removing untracked file tmp/demo.zip
Use case 2: Interactively delete files that are not tracked by Git
Code:
git clean -i
Motivation: Sometimes you want more control over which untracked files to delete. In such cases, you can use the interactive mode of git clean
which allows you to select the files you want to delete.
Explanation: The -i
option of git clean
starts an interactive mode where you can select the files you want to delete. It will show a prompt for each untracked file asking whether to delete it or not.
Example output:
Would remove untracked file test.txt? [n] y
Would remove untracked file tmp/demo.zip? [n] n
Use case 3: Show what files would be deleted without actually deleting them
Code:
git clean --dry-run
Motivation: You may want to preview which files would be deleted by using the git clean
command, without actually deleting them. This is useful when you want to double-check what files will be removed before executing the command.
Explanation: The --dry-run
option of git clean
shows what files would be deleted without actually deleting them. It helps in understanding the impact of the command without making any changes to the working tree.
Example output:
Would remove untracked file test.txt
Would remove untracked file tmp/demo.zip
Use case 4: Forcefully delete files that are not tracked by Git
Code:
git clean -f
Motivation: When you want to forcefully delete untracked files without any prompt, the -f
option of git clean
can be used. It bypasses the interactive mode and directly deletes the files.
Explanation: The -f
option of git clean
forces the deletion of untracked files without any prompt. It is useful when you want to delete all untracked files in one go without being asked for confirmation.
Example output:
Removing untracked file test.txt
Removing untracked file tmp/demo.zip
Use case 5: Forcefully delete directories that are not tracked by Git
Code:
git clean -fd
Motivation: In addition to untracked files, you may also want to forcefully delete untracked directories that are not being tracked by Git. The -d
option of git clean
handles this use case.
Explanation: The -d
option of git clean
is used to forcefully delete untracked directories that are not being tracked by Git. It removes both empty directories and directories containing untracked files and subdirectories.
Example output:
Removing untracked file test.txt
Removing directory tmp/
Use case 6: Delete untracked files, including ignored files in .gitignore and .git/info/exclude
Code:
git clean -x
Motivation: Sometimes you may want to delete not only untracked files but also ignored files that are specified in the .gitignore
and .git/info/exclude
files. The -x
option of git clean
handles this scenario.
Explanation: The -x
option of git clean
is used to delete untracked files, including those specified in the .gitignore
and .git/info/exclude
files. It ensures that all untracked and ignored files are removed from the working tree.
Example output:
Removing untracked file test.txt
Removing untracked file tmp/demo.zip
Removing ignored file secret.key
Conclusion:
The git clean
command is a powerful tool for removing untracked files from the working tree. It provides various options to control which files to delete and allows you to preview the changes before executing the command. Whether you want to delete all untracked files, selectively remove files, or delete ignored files as well, git clean
has got you covered.