How to remove untracked files in git |Video upload date:  · Duration: PT11M6S  · Language: EN

Learn safe ways to preview and delete untracked files in Git using git clean with examples and tips for avoiding data loss.

Start with a calm preview

If you are about to delete things from a repository do not improvise. Git gives you neat ways to see what will go away before you commit to regret. Use git status to get a quick look or run a dry run of git clean to list candidates without deleting anything.

git status --untracked-files=normal
# or a focused dry run
git clean -n
# include directories in the dry run
git clean -nd

Dry runs are your friend

The -n flag shows what would be removed without touching a single file. Think of it as a preview mode where you still get to look brave without taking action. If the output looks reasonable you can go further.

Common cleanup flags explained

  • -n show what would be removed
  • -f force deletion when you are done looking brave
  • -d include untracked directories
  • -x remove files listed in .gitignore as well as untracked files
  • -i interactive mode that lets you pick items one by one
# remove untracked files after a dry run
git clean -f

# remove untracked directories too
git clean -fd

# remove everything including ignored files use with extreme caution
git clean -fdx

# interactive selection when you want to be picky
git clean -i

When to include ignored files

Ignored files are ignored for a reason. They often include build outputs local configs and other noise. If your disk is full or your build artifacts are a mess you can use -x to include .gitignore entries. Warning this can remove files you actually wanted to keep locally.

Interactive mode for the nervous

If your fingers are faster than your judgement try git clean -i. It gives a menu so you can approve deletions one screen at a time. This is the human friendly option for when you want to avoid tragic mistakes and awkward explanations to teammates.

Safety tips every developer should follow

  • Create a temporary branch or stash local changes before you start cleaning
  • Use the dry run first and only add force once you are sure
  • Back up important files outside the repo if you are not 100 percent confident

In short use git status and git clean -n to preview use git clean -f and related flags when you are ready and use interactive mode when you want the elegant agony of choosing what stays. Git gives you the tools to avoid catastrophe so use them and enjoy the illusion of control.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.