Our first reader submitted tip comes from Dav Glass, and it elaborates on a command used to keep your repository nice and tidy. Have a tip you’d like to share? Submit it!

Files and directories may pile up in your working directory that are left over from merges, generated, or perhaps got mistakenly put there. Whatever the case, you don’t need to ignore them in your .gitignore, you just need to remove them. Running

git clean -n -d <path>

will do a ‘dry run’ of the command and show you just what files and folders are going to be removed. Running it on the gitready gives this output:

Would remove _posts/2009-01-16-cleaning-up-untracked-files.textile

Which definitely would be bad if it was removed. I haven’t added the post to the staging area yet, so the file appears as untracked. If you only want files to be cleaned up and not empty directories, take out the -d option. Once you’re sure that you do want to blow away all of those files, doing

git clean -d <path>

actually performs the deletion. If you omit the <path> git will perform it on your current directory. Also, you may need to throw in the -f option depending on permissions and other situations that may crop up.

Cleaning also has a few other helpful options: if you want to perform a normal clean and clear out all the files that git usually ignores, the -x flag will help immensely. If you just want to trash the ignored files and nothing else, use the -X option.

So if we wanted to do a major cleanup:

$ git clean -n -d -x
Would remove .DS_Store
Would remove _posts/.2009-01-16-cleaning-up-untracked-files.textile.swp
Would remove _posts/2009-01-16-cleaning-up-untracked-files.textile
Would remove _site/
Would remove images/.DS_Store
Would remove images/_site/

That command would clean up files listed under the project’s .gitignore file as well as removing other files and directories that aren’t necessary. As always use precaution when running git clean, and make sure to double check what you’re really deleting.

As always, if you know of other related tricks with git clean let us know in the comments!