06 Mar

Cleaning up your Working Copy

It is a common step in build scripts to cleanup the local working copy to ensure that no potentially stale artifacts from previous builds are being used and that files that are needed by the build are being checked in.

Git conveniently allows deletion of all ignored files, as well as files that are neither staged nor committed, like this:

#!/usr/bin/env bash
# Removes everything that is not checked in
git clean -f -x -d

This is good enough for a build server, however on your personal working copy you don’t want to inadvertently delete work you haven’t staged yet. So instead a slightly more involved cleanup procedure can be used. It will actually fail the cleanup if there are unstaged files that are not ignored, and list those files. Otherwise it will just delete the ignored files.

#!/usr/bin/env bash
# Removes everything that is not checked in
set -e
untracked=$(git ls-files --other --exclude-standard)
if [ "$untracked" ]; then
    echo "These files are neither tracked nor ignored:"
    echo "$untracked"
    exit 1
fi >&2
git clean -f -x -d

Leave a Reply

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