2

I need a help with this:

I want to loop the git status code without status code or together status code in one line; I'm using the code below:

# file.sh
files=$(git status --porcelain)
for file in $files; do
    echo $file
done

# OUTPUT
M
example_folder/example_file
M
example_folder_1/example_file_1
M
example_folder_2/example_file_2
....

The problem is the status code always displays, I need to remove the status code or put it together like this:

# LINES EXPECTED
M example_folder/example_file
M example_folder_1/example_file_1
# OR
example_folder/example_file
example_folder_1/example_file_1

My objective is to output an input using console, like this:

files=$(git status --porcelain)
for file in $files; do
    echo $file
    git add $file
    read -p "enter a comment: " comments
    git commit -m "${comments}"
done

The code above is working, but the status code received the comment too, and I need to remove or put it in one line each line modified.

regards.

cpru
  • 78
  • 1
  • 7

2 Answers2

3

--porcelain displays the result in Short Format.

In the short-format, the status of each path is shown as one of these forms

XY PATH
XY ORIG_PATH -> PATH

The 2nd format occurs when ORIG_PATH is renamed to PATH. Use awk to get PATH.

files=$(git status --porcelain | awk '{print $NF}')
for file in $files; do
    echo $file
    git add $file
    read -p "enter a comment: " comments
    git commit -m "${comments}" -- ${file}
done
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
2

You want to write this:

#!/bin/sh

files=$(git status --porcelain | cut -b4-)
for file in $files; do
    echo $file
    git add $file
    read -p "enter a comment: " comments
    git commit -m "${comments}"
done

The cut -b4- trims off the status part of the output.

bk2204
  • 64,793
  • 6
  • 84
  • 100