gpt4 book ai didi

git - 如何使用脚本循环 git status

转载 作者:行者123 更新时间:2023-12-02 19:23:41 24 4
gpt4 key购买 nike

我需要帮助:

我想在一行中循环不带状态代码或一起状态代码的 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

我的目标是使用控制台输出输入,如下所示:

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

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

问候。

最佳答案

--porcelain 显示 Short 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

关于git - 如何使用脚本循环 git status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62688029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com