Skip to content

Quick git tips: cleaning the repository

“sweep it under what?” by psyberartist is licensed under CC BY 2.0

Intro

git is great, and its documentation is no exception. By simply reading the Git Book, one can get quite proficient on git, thanks to the good balance between theory and examples.

However, most times I found myself learning git the harder way, using Google and Stackoverflow and looking for very little and specific pieces of information. While this is great for solving daily problems, it does not really provide the best learning experience. To achieve that, it is necessary to go to the aforementioned Git Book.

In addition to that, the book is filled with very useful commands which can significantly improve your workflow. They are simple, and once you learn them, you will wonder what has been going on with your life before that moment.

I present this blog entry as a way to summarize and collect those commands. Today, we will talk about cleaning the git repository.

Anyway, forget about this blog and go read the Git Book.

Why cleaning the repository?

Because we have always felt the incommensurable pain of trying to hunt a bug by unknowingly testing the same binary over a over again. Don’t do that to yourself. Nuke everything in the repository and start from scratch.

git clean allow you to do exactly that. But there is a caveat. Once executed, your removed files will be gone. Forever. So it is worth checking some healthier alternatives to old school git clean.

Tip 1: see files which would be deleted by doing a dry-run

Only inform about what would be deleted by the clean command, do not actually perform the action.

$ git clean -dxf --dry-run
Would remove src/WorthlessFile.a
Would remove src/ImportantFile.c
Would remove src/ImportantHeader.c
$

Tip 2: using interactive clean

Decide what to do with each file individually or specify patterns for deletion interactively.

$ git clean -dxi
Would remove the following items: 
src/WorthlessFile.a
src/ImportantFile.c
src/ImportantHeader.c 
*** Commands *** 1: clean 2: filter by pattern 3: select by numbers 4: ask each 5: quit 6: help
What now>

Tip 3: stashing instead of cleaning

Remove everything but save it in a stash. Therefore, it can always be later recovered with git stash apply stash@{WHATEVER}.

$ git stash --all
Saved working directory and index state WIP on master: 4352d54 Fix something HEAD is now at 4352d54 fix master branch 
Published inTools

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *