Skip to content

Instantly share code, notes, and snippets.

@matthewmccullough
Created January 20, 2012 19:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewmccullough/1649017 to your computer and use it in GitHub Desktop.
Save matthewmccullough/1649017 to your computer and use it in GitHub Desktop.
Bash script to remove any ignored files that are accidentally tracked
#!/bin/sh
# Git Remove (from version control) any file that is covered by the ignore patterns and exists on the filesystem. Could be improved to further check the file exists on the git filesystem too.
for eachthing in `git ls-files --others --ignored --exclude-standard`; do if [ -e "$eachthing" ]; then git rm $eachthing; fi; done
@cbeams
Copy link

cbeams commented Jan 21, 2012

Interesting - what does this do that git clean -dfx or git clean -dfX (or some other git clean combo) couldn't?

@matthewmccullough
Copy link
Author

@cbeams This ls-files lists the ignored files in a decoration-less way that can be piped to a git rm command. Clean would wipe them out, but then you'd have to stage the removals. And then, what if you didn't want to stage some other changed (but not removed) files? git add -u . would gum you up there by grabbing files you didn't want to stage. Let me know if that helps explain. Would like to sharpen the message for other consumers of the script as much as I can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment