DEV Community

olistik
olistik

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 STDIN.read.lines.map(&:split).map(&:last).join(' ')")
Enter fullscreen mode Exit fullscreen mode

Done

Image source

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

Discussion (2)

Collapse
kwstannard profile image
Kelly Stannard

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

Collapse
olistik profile image
olistik Author

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