GitClean is a cross-platform PowerShell module that provides an Invoke-GitClean cmdlet (with a Git-Clean alias) to easily perform a git clean -xdf on all git repositories under a specified directory in order to free up disk space.
Git repositories with untracked files will not be cleaned by default, to avoid losing any work that has not yet been committed.
Warning
Do not run this command if you have files in your .gitignore that you want to keep, such as workspace or environment configuration files.
e.g. The .vscode directory or .env files.
Ideally those files should exist outside of your git repository when possible, such as in your user profile or environment variables.
If you enjoy this project, consider giving it a GitHub star β to show your support.
Developers often have 10s or 100s of git repositories cloned on their local machine. We don't always remember to clean up build artifacts and temporary files when we are done, such as NuGet packages and node_modules. These files can take up a lot of space on your hard drive. This module provides a simple way to clean up all of your git repositories at once, allowing you to reclaim potentially GBs of disk space.
Here is a screenshot of running Invoke-GitClean with no parameters, so it scans the current directory and subdirectories for git repositories and cleans them:
It found and cleaned 15 git repositories, reclaiming 461 MB of disk space in 1 minute and 14 seconds.
Here is an example of explicitly providing the root directory path to scan for git repositories and displaying additional informational messages:
It found 262 git repositories, 2 of which had untracked files so they were not cleaned. By cleaning the other 260, it reclaimed 33.4 GB of disk space in 15 minutes and 18 seconds.
To install the module from the PowerShell Gallery, run the following command:
Install-Module -Name GitClean -Scope CurrentUserTo clean all git repositories under a specified directory, run the following command:
Invoke-GitClean -RootDirectoryPath 'C:\path\to\repositories'This assumes that there are multiple git repositories under the specified root directory.
Note
To avoid accidentally deleting files that have not yet been committed to git, this cmdlet will only clean repositories that have no untracked files.
This ensures you don't lose any work that you haven't committed yet.
Use the -Force parameter to bypass this safety check.
Warning
Do not run this command if you have files in your .gitignore that you want to keep, such as workspace or environment configuration files.
e.g. The .vscode directory or .env files.
Ideally those files should exist outside of your git repository when possible, such as in your user profile or environment variables.
This module only provides one cmdlet: Invoke-GitClean (aliases: Git-Clean, Clean-GitRepositories)
It accepts the following parameters:
| Parameter | Description | Alias | Default |
|---|---|---|---|
RootDirectoryPath |
The root directory to search for git repositories in. If not provided, the current directory will be used. | Path |
Current directory |
DirectorySearchDepth |
The depth to search for git repositories under the RootDirectoryPath. A large value may increase the time it takes to discover git repositories. |
Depth |
3 |
Force |
If provided, all git repositories will be cleaned, even if they have untracked files. Be careful with this switch! |
The following common parameters are also supported:
| Parameter | Description |
|---|---|
WhatIf |
If provided, the cmdlet will not actually delete any files. It will only show you which repos would be cleaned, even if -Force is provided. |
Confirm |
If provided, the cmdlet will prompt you to confirm before cleaning each repository. |
InformationAction |
If provided, the cmdlet will output general information about what it is doing. |
Verbose |
If provided, the cmdlet will output verbose information about what it is doing. |
The cmdlet returns a PSCustomObject with the following properties:
| Property | Description |
|---|---|
RepositoryPath |
Path that was used to run the command. |
DirectorySearchDepth |
Depth that was used to run the command. |
NumberOfGitRepositoriesFound |
Number of git repositories that were found. |
GitRepositoriesCleaned |
Array of the git repository directory paths that were cleaned. |
GitRepositoriesWithUntrackedFiles |
Array of the git repository directory paths that were not cleaned due to having untracked files. |
Duration |
How long the operation took to complete. |
DiskSpaceReclaimedInMb |
The amount of disk space in megabytes that was reclaimed by the git clean operations. |
Clean all git repositories under the current directory:
Invoke-GitCleanClean all git repositories under the current directory, using an Invoke-GitClean alias:
Clean-GitRepositoriesor
Git-CleanDo not clean any repositories, but show which ones would be cleaned:
Invoke-GitClean -RootDirectoryPath 'C:\path\to\repositories' -WhatIfClean all repositories, even if they have untracked files:
Invoke-GitClean -Path 'C:\path\to\repositories' -ForcePrompt the user for confirmation before cleaning each repository, and use the Git-Clean alias:
Git-Clean -Path 'C:\path\to\repositories' -ConfirmWith repositories at the following paths:
C:\path\to\repositories\repo1C:\path\to\repositories\repo2C:\path\to\repositories\OtherRepos\repo3C:\path\to\repositories\OtherRepos\repo4C:\path\to\repositories\OtherRepos\MoreRepos\repo5
Only clean repo1 and repo2:
Invoke-GitClean -Path 'C:\path\to\repositories' -Depth 1And to only clean repo1, repo2, repo3, and repo4:
Invoke-GitClean -Path 'C:\path\to\repositories' -Depth 2List all git repositories that were not cleaned because they have untracked files:
$result = Invoke-GitClean -Path 'C:\path\to\repositories'
$result.GitRepositoriesWithUntrackedFilesShow general and verbose information about what the cmdlet is doing:
Invoke-GitClean -Path 'C:\path\to\repositories' -InformationAction Continue -VerboseIssues and Pull Requests are welcome. See the Contributing page for more details.
See what's changed in the application over time by viewing the changelog.
Buy me a milkshake for providing this PowerShell module open source and for free π


