Clean Your Git Repository With Git Clean
When your Git repository fills up with unnecessary files, it is time to clean up the mess.
To remove the files that Git is not tracking (but not the files that Git ignores using '.gitignore'), run the following command:
git clean
That's it!
Git will now remove all the files that are not being tracked and your disk space will be re-claimed again.
This is only one task that git clean
command performs. To learn more about this command, you need not look elsewhere!
Git clean
The command git clean
is a subset command of Git - a version control system.
The git clean
command, when run without any arguments/flags will recursively delete all the files that Git does not track, but it will exclude files that are included in '.gitignore'.
There are several arguments/flags that are available for you to use with git clean
. They are as follows:
-n
[or]--dry-run
: Do not remove any files and/or directories, but show what would be done. An excellent flag to prevent unintentional file deletion.-d
: When no path is specified, Git will not recursively delete files from any untracked directory. This option is helpful if you are not sure about every untracked file but still want to delete untracked files.-i
[or]--interactive
: Get details of what operations would be carried out - approve and disapprove them.-f
[or]--force
: Git has a configuration variableclean.requireForce
, which, if set tofalse
, Git will refuse to delete anything. The-f
flag is used to override that variable.-q
[or]--quiet
: Don't output anything except for errors.
Let's look at an example where I have 'README.md' file under Git's version control, but not 'untracked-file' file and neither 'untracked-dir' directory.
$ ls -l1
total 8
-rw-rw-r-- 1 pratham pratham 55 Mar 2 13:56 README.md
drwxrwxr-x 2 pratham pratham 4096 Mar 2 13:57 untracked-dir
-rw-rw-r-- 1 pratham pratham 0 Mar 2 13:57 untracked-file
$ git clean -n
Would remove untracked-file
$ git clean -dn
Would remove untracked-dir/
Would remove untracked-file
$ git clean -f
Removing untracked-file
# touch 'untracked-file' after git deleted it
$ git clean -df
Removing untracked-dir/
Removing untracked-file
There are three items in my Git repository. I have two files 'README.md' and 'untracked-file' and a directory 'untracked-dir'. Out of these three items, only 'README.md' is being monitored by Git for changes.
So, when I tell git clean to do a dry run without additional flags, I can see that only the 'untracked-file' file will be deleted, not the directory too. But when I use the -d
flag along with -n
flag, now I see that Git will delete the directory (and it's contents, if it had any) too.
This can be confirmed by the use of -f
flag instead of -n
flag.
Running git clean -f
only removed the 'untracked-file' file. (I also created it again using the touch
command for the sake of this guide)
Now, when I run git clean -df
, Git will now recursively delete untracked files and directories. So that removes both untracked items, which are 'untracked-file' file and 'untracked-dir' directory.
Conclusion
Git has a sub-command git clean
that removes any untracked files that may be unnecessarily hogging up storage space on your drive. Use git clean
to remove said items.
To learn more about Git, you can head over to our main site.