Little Git Tricks: Clean-up Your Working Directory

If your code works on the Continuous Integration (CI) server but not on your local machine, you may have a problem in the files not tracked by Git. And since Git does not track them, there is no easy way to get rid of them – at least in most Git clients. In such cases, we can go to the command line and use the full feature set of Git to fix the problem.

Attention: make sure that you commit or stash all your changes – otherwise they get deleted!

Open the command line and go to the top folder of your repository (the one with the .git folder). You can run this command to remove all untracked files in your repository:

The d option tells git-clean to include the subdirectories, f forces Git to remove the files and x ignores the exclude lists of .gitignore. If everything works without an error, your local copy will look the same as when you cloned your repository.

Should you try this Git command while Visual Studio runs with your project, you will get a lot of errors about files used by another process. In this case stop Visual Studio and run git clean again.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.