Removing untracked files but not git ignored ones

Quite often you drop a load of files in to a git repo, such as a Magento extension, and then immediately regret it. But now you have a pile of files scattered across the repo and no way to Ctrl+Z out of the situation.

This leaves us with some options:

The best way is to automate the rm -rf procedure mentioned above using the following command

git status --porcelain | sed 's#^...##' | while read f; do rm -rf $f; done

The TL;DR of this command is that it outputs git status in a parseable way, and then loops over each file/folder and deletes it. You can see a full command breakdown here.


Tags: gituntrackedbash