Git Clean

This command is particularly useful when you want to remove unnecessary or ignored files from your working directory. We'll explore various ways to use the git clean command, providing you with a comprehensive guide to maintaining a clean and organized project structure.

1. Basic Clean: Removing Untracked Files:

The fundamental use of git clean involves removing untracked files from your working directory:

bash
git clean

By default, this command removes untracked files, but not directories. It provides a clean slate, ensuring that only tracked files remain.

2. Dry Run: Previewing Changes without Removing Files:

To preview the changes that git clean would make without actually removing anything, use the -n or --dry-run option:

bash
git clean -n

This allows you to see which files would be deleted without executing the cleanup.

3. Force Clean: Removing Untracked Files and Directories:

If you want to remove both untracked files and directories, use the -f or --force option:

bash
git clean -f

This cleans up untracked files and directories, providing a more thorough cleanup.

4. Ignoring Files and Directories:

To exclude certain files or directories from being removed by git clean, create a .gitignore file and add patterns for files or directories you want to ignore:

bash
echo "file-to-ignore" > .gitignore git clean -fX

The -X option ensures that only files matching patterns in .gitignore are removed.

5. Ignoring Files Based on Patterns:

To clean up files based on patterns, use the -e option:

bash
git clean -e "pattern-to-keep"

This command excludes files matching the specified pattern from being removed.

6. Interactively Choosing Files to Clean:

For a more interactive cleanup process, use the -i or --interactive option:

bash
git clean -i

This prompts you to choose which files to clean, providing a safer approach to removing untracked files.

7. Removing Untracked Directories:

To remove untracked directories as well, use the -d option:

bash
git clean -fd

This removes both untracked files and directories, giving your working directory a thorough cleanup.

8. Removing Only Ignored Files:

If you want to clean up only files that are ignored by Git, use the -X option:

bash
git clean -X

This command removes only files that are ignored by Git according to .gitignore.

9. Removing Ignored Files and Directories:

To clean up both ignored files and directories, combine the -X and -d options:

bash
git clean -fdX

This ensures a comprehensive cleanup, removing untracked files and directories while respecting your ignore rules.

10. Specifying a Specific Directory:

If you only want to clean up a specific directory within your project, provide its path as an argument:

bash
git clean -fdX path/to/directory

This command limits the cleanup to the specified directory.

11. Removing Files Matching a Pattern:

To remove files matching a specific pattern, use the -e option with a pattern:

bash
git clean -e "*.txt"

This command excludes files matching the specified pattern from the cleanup.

12. Ignoring Files Locally:

To ignore files only locally (without modifying .gitignore), use the -x option:

bash
git clean -fx

This ignores files locally for the current cleanup operation.