bashgitshelldirectorygit-status

Bash - Get only directory names from git status


I just need the directory names from the git status. As a short form when I use git status -s, I get the following output.

M stack/pipeline.yaml
?? stackother/

Whereas I just need stack/ and stackother/ as a list or just as an output which I can pipe to the next set of commands.

Basically, I need the directories which are newly created or directories in which there are some changes in the files. Is there any way to do it using git?

EDIT: This question is not a duplicate of the linked question as I actually needed the parent directory of each changed file. The following example demonstrates what is needed and what the linked questions answers:

Given these files: stack/pipeline.yaml stack/newstack/master-pipeline.yaml stack/newstack/test/test11.txt stack/newstack/test/tester.txt

I need:

stack
stack/newstack
stack/newstack/test

and not (provided by the answer in the linked question):

stack

For the basic example, the answer with porcelain worked, but for more changes, I had to perform git status -s -uall and work on this output.


Solution

  • You can do it as follow:

    git status -s -uall | awk '{print $2}' | xargs -L1 dirname| uniq