如何使用脚本循环 git 状态

How to loop git status using scripting

我需要帮助:

我想循环 git 没有状态码的状态码或一行中的状态码;我正在使用以下代码:

# 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
....

问题是状态码一直显示,我需要把状态码去掉或者像这样放在一起:

# 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

我的objective是使用控制台输出一个输入,像这样:

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

上面的代码可以运行,但是状态代码也收到了评论,我需要在修改的每一行中将其删除或放在一行中。

问候。

你要这样写:

#!/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

cut -b4- 修剪掉输出的状态部分。

--porcelainShort Format 中显示结果。

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

XY PATH
XY ORIG_PATH -> PATH

ORIG_PATH 重命名为 PATH 时出现第二种格式。使用 awk 得到 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