1

Consider the following command:

git clean -n -x -d

This produces output of the following form:

Would remove path/to/file/1
Would remove untracked/directory

I would like to run a different command X on these files, but the extra Would remove prefix is not useful.

I could run sed or cut to kill this prefix.

 git clean -n -x -d  | cut -c 14-

However, I'm curious about whether there's a lower level git plumbing command I can use the get the output I want without starting another process.

git ls-files --other seemed promising, but unfortunately, its output includes all the files, instead of just the directory containing all the files.

path/to/file/1
untracked/directory/file1
untracked/directory/file2
...

Is there a low-level git command to replicate the output of git clean above without the extra prefix, or is my cleanest option just to cut out the prefix?

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • I would use `awk`. `git clean -n -x -d | awk '{print $3}'`? – evolutionxbox Dec 08 '21 at 10:17
  • Doing `| cut -c 14-` seems not smart, seeing the output (from `git clean`) in this question: https://stackoverflow.com/questions/8638562/git-clean-what-does-would-not-remove-mean – Luuk Dec 08 '21 at 10:21
  • 1
    @evolutionxbox: And hope your filenames do NOT contain spaces. – Luuk Dec 08 '21 at 10:21
  • @evolutionxbox only works with some locales ;) "Would remove" could be more or less than 2 words in other languages. – YSC Dec 08 '21 at 10:22
  • @Luuk hahaha. _live on the edge_ – evolutionxbox Dec 08 '21 at 10:22
  • If you really want to use awk, use `gawk` and do: `echo Would remove a b c d | gawk '/^Would remove/{ $1=null; $2=$1; FS="\x00"; $0=$0; gsub(/^ */,"",$1); }1'`, to get the filename with spaces as output. – Luuk Dec 08 '21 at 10:44
  • @Luuk The warning is much appreciated! – merlin2011 Dec 08 '21 at 17:43
  • Side note: `git clean` does this internally, not by running a separate plumbing command. Fortunately `git ls-files`, as a separate plumbing command, has the capabilities you want. Note that `git clean` does however play tricks with ignored files (`-x` vs `-X`), so to cover everything gets complicated. – torek Dec 08 '21 at 22:00
  • @torek Are you implying that the second command in the answer will not always produce the same output as my command above? – merlin2011 Dec 09 '21 at 01:18
  • @merlin2011: `git clean -fX` (you're not using uppercase X, but I'm trying to cover all cases here) removes *only* **ignored** files, so it does not remove files printed as `??` by `git status --short`, for instance. Meanwhile `git status -x` skips the `.gitignore` files, so that no files are ignored (unless you add `-e` arguments, which are another way `git clean` can do weird special cases). – torek Dec 09 '21 at 02:48
  • Besides this, `git status` says nothing about empty directories (those with no files in them), but `git clean -d` will remove them. I'm pretty sure `git ls-files --others` never mentions empty directories either. Let me test it... yes, `git clean -ndx` prints `Would remove empty/` but neither `git ls-files` nor `git status` mention it (under any circumstances). – torek Dec 09 '21 at 02:52

1 Answers1

1

If you need to get near git's plumbing, you can list untracked files with:

git ls-files --others

Alternatively, you can stick with commonly known tools, i.e.

git status --short --ignored | grep '!!\|??'

Documentation:

YSC
  • 38,212
  • 9
  • 96
  • 149