mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add Remove Untracked Files From A Directory as a Git TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1425 TILs and counting..._
|
||||
_1426 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -321,6 +321,7 @@ _1425 TILs and counting..._
|
||||
- [Quicker Commit Fixes With The Fixup Flag](git/quicker-commit-fixes-with-the-fixup-flag.md)
|
||||
- [Rebase Commits With An Arbitrary Command](git/rebase-commits-with-an-arbitrary-command.md)
|
||||
- [Reference A Commit Via Commit Message Pattern Matching](git/reference-a-commit-via-commit-message-pattern-matching.md)
|
||||
- [Remove Untracked Files From A Directory](git/remove-untracked-files-from-a-directory.md)
|
||||
- [Rename A Remote](git/rename-a-remote.md)
|
||||
- [Renaming A Branch](git/renaming-a-branch.md)
|
||||
- [Resetting A Reset](git/resetting-a-reset.md)
|
||||
|
||||
26
git/remove-untracked-files-from-a-directory.md
Normal file
26
git/remove-untracked-files-from-a-directory.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# Remove Untracked Files From A Directory
|
||||
|
||||
Let's say I have a directory (`spec/cassettes`) full of a ton of generated YAML
|
||||
files. Most of these files are tracked by git. However, I just generated a
|
||||
bunch of new ones that are untracked. For whatever reason, I don't want these
|
||||
files. I need to delete them.
|
||||
|
||||
Running `rm` on each of them is going to be too tedious. And it is tricky to
|
||||
target them for a bulk delete since there are a ton of other files in that
|
||||
directory that I want to keep.
|
||||
|
||||
One way to approach this is have `git ls-files` help out with listing all files in the
|
||||
directory that are untracked. The `--others` flag filters to untracked files.
|
||||
|
||||
```bash
|
||||
git ls-files --others --exclude-standard spec/cassettes
|
||||
```
|
||||
|
||||
From there, I can pipe it to `rm` (with `xargs` collapsing all the files into a
|
||||
single line):
|
||||
|
||||
```bash
|
||||
git ls-files --others --exclude-standard spec/cassettes | xargs rm
|
||||
```
|
||||
|
||||
See `man git-ls-files` for more details.
|
||||
Reference in New Issue
Block a user