get clean

January 14, 2014

I experiment a lot before I choose an implementation I like. I am sure I am not alone in making a few (or many) false starts, and yet feeling sick at the thought of deleting any code. What if one of those paths I started down was the right one? This loss aversion often leads to working trees of non-working code, and git stash-es that I will never revisit. If I stop myself from going too far down yet another road without an end in sight, attempts to clean up my working directory often stall miserably at the end of a git checkout .:

On branch intelligent_branch_name
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# ../test/yay_I_wrote_tests.rb
# ../helpers/yay_I_wrote_a_helper.rb
# ../helpers/how_many_helpers_did_I_write.rb
# ../views/i_dont_remember_creating_this_file.rb
# ../models/oh_thats_where_that_went.rb

Once upon a time, I would click my way through rm after rm. A small improvement in rm efficiency came when I discovered I could rm more than one file at a time (don’t judge me, I am new at this). Still, the biggest break-through came when a coworker, perhaps observing my struggles, pointed me to git-cheatsheet — a interactive visual tool to display the relevant commands for each stage of my git workflow.

Enter git clean

Git clean is a force to be reckoned with, and not just because it doesn’t do anything unless you force it.

Use the -n flag to describe the action the cleaner would take

$ git clean -n
Would remove ../helpers/yay_I_wrote_a_helper.rb
Would remove ../helpers/how_many_helpers_did_I_write.rb
Would remove ../views/i_dont_remember_creating_this_file.rb
Would remove ../models/oh_thats_where_that_went.rb
$

Use the -f flag to force the cleanup

$ git clean -f
removed ../helpers/yay_I_wrote_a_helper.rb
removed ../helpers/how_many_helpers_did_I_write.rb
removed ../views/i_dont_remember_creating_this_file.rb
removed ../models/oh_thats_where_that_went.rb
$

Now I can be as crufty as I want to be in my working tree, because git will be there to clean up the mess.

Awesome.


Katie Leonard

Mostly Katie explaining things to herself.

© 2025