用于使用bash / shell脚本添加和提交的Git命令

问题描述 投票:0回答:1

我是bash脚本的新手,我正在尝试编写一个添加,提交和推送到存储库的脚本

commit_message="$1"
git add . -A
git commit -m "$commit_message"
git push

这会将所有已编辑/新文件添加到我的仓库中,有没有办法将所需的文件名作为执行此脚本的参数传递?我从谷歌那里得到了这个剧本,但如果还有其他办法,我可以告诉我。

bash git git-bash
1个回答
2
投票

为方便起见我使用了一个功能。它适用于我的编码风格,对我来说,这意味着始终在repo根目录下的干净目录中工作,并使用相对路径访问所有文件。因人而异。

qp() {
    [[ -z "$1" ]] && echo "Please enter a commit message:";
    typeset msg="$( [[ -n "$1" ]] && echo "$*" || echo $(head -1) )";
    date;
    git pull;
    git add .;
    git commit -m "$msg";
    git push;
    date
}

称之为 -

qp add a commit message

请注意,它将所有参数展平为单个msg,如果没有,则会提示输入一个。

$: qp
Please enter a commit message:
foo bar baz
Tue, Mar 19, 2019  3:25:24 PM
Already up-to-date.
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean
Everything up-to-date
Tue, Mar 19, 2019  3:25:31 PM

What you asked for:

重写它以获取文件列表并始终询问消息,如下所示:

qp() {
    echo "Please enter a commit message:";
    typeset msg="$( head -1 )";
    date;
    git pull;
    git add "$@";
    git commit -m "$msg";
    git push;
    date
}

您可以根据需要将功能代码放入带或不带功能的脚本中。

然后运行它

qp file1 file2 fileN

并且它将要求提交消息 - 或者,使第一个参数成为提交消息,如下所示:

qp() {
    typeset msg="$1";
    shift;
    date;
    git pull;
    git add "$@";
    git commit -m "$msg";
    git push;
    date
}

只要确保你“引用”第一个提交消息参数。 ;)

qp "here's my commit message" file1 file2 fileN
© www.soinside.com 2019 - 2024. All rights reserved.