DEV Community

Cover image for Cleaning your working directory in Git (including untracked files) ๐Ÿ˜Ž
Weverton Timoteo
Weverton Timoteo

Posted on

Cleaning your working directory in Git (including untracked files) ๐Ÿ˜Ž

Have you ever found yourself in a Git repo mess with unwanted files and changes piling up? Don't sweat it; we've got your back! Let's roll up our sleeves and learn how to clean things up and get back to coding in style. Plus, we'll show you some cool Git tricks along the way! ๐Ÿ˜„

Step 1: Clear Out the Clutter ๐Ÿงน

Git's clean command is like your personal repo janitor. It sweeps away all those pesky untracked files and directories. Open your terminal and head to your Git playground. Then, hit it with this command:

git clean -f -d
Enter fullscreen mode Exit fullscreen mode

Here's the lowdown on the command:

  • git clean: The cleanup wizard.
  • -f: The "force" flag. It means we're going all-in on the cleanup without asking too many questions.
  • -d: The "directories" flag. This tells Git to clean up directories along with files, so you get a squeaky-clean workspace.

Step 2: Say Goodbye to Local Changes โœŒ๏ธ

With the clutter gone, you might still have some local changes you want to ditch. We're gonna use the checkout command to give those changes a one-way ticket out of your repo:

git checkout .
Enter fullscreen mode Exit fullscreen mode

This command throws away local changes and brings your repo back to the way it was when you last committed. Fresh start, anyone? ๐Ÿš€

Bonus: Get Interactive! ๐Ÿ’ก

Now, for the cherry on top: Git's interactive mode. It's like a toolbox full of magic wands for precise cleaning. Let's break it out and see the sparks fly:

git clean -i
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ฎ Poof! You're in Git's interactive menu. This menu is where the real fun begins. It gives you the power to choose which untracked files or changes stay and which ones go. It's like cleaning with a fine-toothed comb.

And there you have it! Your Git repo is looking sharp and ready for more epic coding adventures. Just remember to keep your valuable changes safe before you unleash Git's cleanup magic.


Cleaning up your Git repo doesn't have to be a chore. With Git's clean and checkout commands, along with the bonus interactive mode, you're in control. Keep your repo organized and fresh, and you'll be coding like a pro in no time! ๐Ÿ’ป๐Ÿš€

Top comments (0)