2

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.

Biplob Biswas
  • 1,761
  • 19
  • 33

1 Answers1

1

You can do it as follow:

git status -s -uall | awk '{print $2}' | xargs -L1 dirname| uniq
padawin
  • 4,230
  • 15
  • 19
  • I understand what you did here, but somehow there's a space before the M character and it outputs M as an output for the first line, for the second line its good – Biplob Biswas Apr 29 '19 at 13:33
  • What about with `awk '{print $2}'` instead of `cut -d ' ' -f 2` ? – padawin Apr 29 '19 at 13:35
  • git status -s -uall | cut -c 4- | xargs -L1 dirname, this worked for me. Also, git status --porcelain | awk '{print $2}' | xargs -L1 dirname Please update your answer so I can accept it :) – Biplob Biswas Apr 29 '19 at 14:27
  • Updated with awk :-) – padawin Apr 29 '19 at 14:33
  • Actually, the command with porcelain doesn't fit my need because I needed the parent directory for each file change and for my given example the porcelain answer worked as well. I have updated the question as its been marked as duplicate when its not. `git status -s -uall | awk '{print $2}' | xargs -L1 dirname| uniq` does what i need. May be update the answer accordingly. – Biplob Biswas Apr 30 '19 at 14:16
  • @padawin Cool. The command in this comment section worked. But is there a way to list out the main directory only, not the sub-directory?! The uniq command is cool. – Pranav Mar 20 '22 at 06:50