Skip to content

Instantly share code, notes, and snippets.

@brock
Created April 19, 2017 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brock/46ec839c859b1c4c1d35add1bfe27eff to your computer and use it in GitHub Desktop.
Save brock/46ec839c859b1c4c1d35add1bfe27eff to your computer and use it in GitHub Desktop.
GRU: Git Remove Untracked Files
#!/bin/bash
# gru
# Git Remove Untracked (files)
# without this script, you have to manually delete files and directories in your git directory if
# you want to delete them and they are not tracked by git.
# this works as an alias or an executable file
# Explanation:
# git status -s (display the git status of each file, one line at a time, in short format)
# grep '^??' (untracked files will begin with ??)
# cut -d\ -f2- (get the path to the file, explained here: http://stackoverflow.com/a/9004039/2083544)
# xargs -I \{\} rm -rf "{}" (pipe the filepath or directory to the remove command)
git status -s | grep '^??' | cut -d\ -f2- | xargs -I \{\} rm -rf "{}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment