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

Git commands for adding and commiting using bash/shell script

我完全不熟悉 bash 脚本,我正在尝试编写一个脚本来添加、提交和推送到存储库

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

这会将所有 edited/new 文件添加到我的存储库中,有没有办法将所需的文件名作为执行此脚本的参数传递? 我从 google 那里得到了这个脚本,但是如果我有任何其他方法可以做到这一点,请告诉我。

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

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

这样称呼它 -

qp add a commit message

请注意,它将所有参数扁平化为一个 msg,如果它得到 none,它会提示输入一个。

$: 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
}

您可以将功能代码放入脚本中,有或没有您喜欢的功能。

  • 注意:您可能更喜欢 cat 而不是 head -1 以允许多行,但您必须使用 [=45= 终止您的消息] 或类似的东西。

然后运行它作为

qp file1 file2 fileN

它会要求提供提交消息——或者,将第一个参数设为提交消息,如下所示:

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

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

qp "here's my commit message" file1 file2 fileN