DEV Community


Posted on • Updated on

Delete all untracked files in git

Suppose that you've just done a git checkout -f to cleanup your local modifications.

New files added will slip out of the previous command, because they're treated by git as "untracked".

Here's a one-liner to get rid of these little fellas (requires a Ruby interpreter, because I <3 Ruby):

git status \
  --untracked-files=all \
  --no-column \
  --short | \
  rm $(ruby -e "puts' ')")
Enter fullscreen mode Exit fullscreen mode


Image source

Update: as @kwstannard rightfully pointed out in the comments, there's also the prettier git-clean.

Discussion (2)

kwstannard profile image
Kelly Stannard

Nice! You may be interested in the git-clean command as well.

olistik profile image
olistik Author

You're definitely right. I didn't know it existed. ^_^'