Completely cleaning up git workspace

When developing software using git as version control system, sometimes there is the wish to completely reset your workspace, so that your snapshot is exactly as the snapshot in the repository. There shall no deleted, missing or additional files or folders that are not tracked. Furthermore git does not track folders. There shouldn’t any dead folders that git doesn’t care for. Let’s have a look how to do that.

In order to completely wipe your workspace and to get to the snapshot of a defined commit it is necessary to do the following steps:

Resetting workspace

git per default handles all the files that are tracked. So git doesn’t care about untracked files.

git reset --hard

This enforces git to completely rollback all changes to tracked files and also resets the index. git offers different options for the command reset.

A small overview of the most common git reset parameters. Per default --mixed is applied. Only one of them can be used at a time.

--soft resets the HEAD
--mixed resets the index additionally to --soft
--hard resets the working folder additionally to --mixed

After that the head, the index and all tracked working files are in its original state.

Cleaning up workspace

The second step is to clean up the workspace. This means purging all the files and folders that are not tracked. For that there is a command called git clean.

git clean -df

The option -d also removes directories additionally to files.
Furthermore the -f forces git to clean in case the config property clean.requireForce is not set to false.

Adding alias for git command

For convenience an alias might be a good idea. Simply have a look into the git-config documentation. With the syntax alias.* an alias can be defined. In order to put it into the global git configuration simply apply the following command:

git config --global alias.rc "! git reset --hard && git clean -df"

In this case an alias rc is defined. So you can call git rc in order to reset and then clean the workspace in one step.

Happy coding!

Sources

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.