Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create string array from git status bash output

Tags:

git

bash

I am trying to create a string array of my modified git files so I can use them in a bash program. Sample output:

On branch restructured
Your branch is up-to-date with 'origin/restructured'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified: path/to/file1 
    modified: path/to/file2

I'm tryig to grab the text after modified: but from what I've found grep doesn't support new line so i'm at a loss how I could convert the raw output into something I could work with.

like image 880
Nick Avatar asked Oct 23 '25 04:10

Nick


2 Answers

If you really just want a list of modified files, consider git ls-files -m. If you need something extensible to potentially other types of changes:

git status --porcelain | while read -r status file; do
  case "$status" in 
    M) printf '%s\n' "$file";;
  esac
done
like image 134
kojiro Avatar answered Oct 25 '25 17:10

kojiro


How about:

files=(git status | grep '^\s*modified:' | cut -f 2- -d :)

Reading from inside out, that:

  • Passes git status to grep, which
  • looks for lines with modified: on them singularly, then
  • cuts everything after the colon on those lines, then finally
  • puts that into an array assigned to $files
like image 23
bishop Avatar answered Oct 25 '25 18:10

bishop