Git 提交 bash 脚本

47

我正在编写一个Bash脚本,用于将目录中的所有文件添加、提交、推送到远程仓库。

#!/bin/bash  
git add .  
read -p "Commit description: " desc  
git commit -m $desc  
git push origin master

我遇到了以下错误:

$ ./togithub  
Commit description:   
test commit script  
error: pathspec 'commit' did not match any file(s) known to git.  
error: pathspec 'script"' did not match any file(s) known to git.  
Everything up-to-date

我不确定这是否是读入文本的问题(它可以正常地echo),或者将其传递给git commit -m的问题。

7个回答

60

你需要做:

git commit -m "$desc"

在当前脚本中,test 作为提交信息,而 commitscript 则被视为下一个参数。


3
“正确引用”这一点永远不会被过度强调。网络上有太多低劣的“如何做”的指南以及半正确的建议/示例...请注意适当引用。 - jørgensen
如果您将整个命令作为参数传递,可以使用反斜杠转义引号,例如 ./myscript.sh git commit -m \"my commit message\" - chuckwired

20

这是最后两个答案的合并 - 串联 add -u 是很棒的,但嵌入式 read 命令给我带来了麻烦。我选择了以下方法(如果你使用的是 "git push origin head",将最后一行更改为该命令):

#!/bin/bash
read -p "Commit description: " desc
git add . && \
git add -u && \
git commit -m "$desc" && \
git push heroku master

只需执行 git add -A . 而不是 git add . && git add -u - dǝɥɔS ʇoıןןƎ

7

将实际已删除的文件从索引中删除非常有用。git add -u可以解决这个问题。此外,您可能希望像这样链接这些命令:

git add . && \
git add -u && \
git commit -m "$(read -p 'Commit description: ')" && \
git push origin HEAD

如果任何命令失败,它将停止评估剩余的命令。

只是给你一些思考(未经测试的食物)。

谢谢!


3
您还可以使用 #!/bin/bash -e 来使脚本在任何命令失败时退出。 - bluegray
3
很有趣,这个得到了很多赞...你会得到空的提交信息... - gniourf_gniourf

6
#!/bin/bash  
git pull
git add .
git commit -m "$*"
git push

使用注释作为命令参数来调用脚本,减少按键次数:

$ ./togithub test commit script 

5
以下是我用来管理git仓库的脚本-这将包括推送到您的原始分支、暂存站点(如果设置)和生产站点(如果设置)。
#!/usr/bin/env bash

# die script -- just in case
die() { echo "$@" 1>&2 ; exit 1; }

# kill message when dead 
 KILL="Invalid Command"

# function to see where to push what branch
pushing() {
    git branch
    sleep 1
    tput setaf 1;echo  What Branch?;tput sgr0 
    read -r branch
    tput setaf 2;echo  Where to? You can say 'origin', 'staging', or 'production';tput sgr0 
    read -r ans
    if [ "$ans" = "origin" ] || [ "$ans" = "staging" ] || [ "$ans" = "production" ]
    then
        git push "$ans" "$branch" 
    elif [ "$ans" = "no" ]
    then
        echo "Okay" 
    else die "$KILL"
    fi
}

# function to see how many more times
more() {
    tput setaf 2;echo More?;tput sgr0 
    read -r more
    if [ "$more" = "yes" ]
    then
        pushing
    elif [ "$more" = "no" ]
    then
        die "Goodbye" 
    else die "$KILL"
    fi
}

# get the root directory in case you run script from deeper into the repo
gr="$(git rev-parse --show-toplevel)"
cd "$gr" || exit
tput setaf 5;pwd;tput sgr0 

# begin commit input
git add . -A
read -r -p "Commit description: " desc  
git commit -m "$desc"

# find out if we're pushin somewhere
tput setaf 2;echo  wanna do some pushin?;tput sgr0 
read -r push 
if [ "$push" = "yes" ]
then 
    pushing # you know this function 
    until [ "$more" = "no" ]
    do
        more # you know this function
    done
elif [ "$push" = "no" ]
then
    echo "Okay" 
else die "$KILL"
fi

我尽可能地添加了大量注释,以帮助您理解每个部分的作用。

如果您有任何问题,请告诉我。

此外,我将其设置如下:

echo "alias commit='sh /path/to/script.sh" >> ~/.bash_profile source ~/.bash_profile

也许这可以帮助那些想加速工作流程的人。


2

这是我大部分时间用来提交本地分支和合并远程分支的方法:

这个小的bash脚本可以让您在本地分支上添加和提交,检出到另一个分支,与之合并并推送它,然后再次检出到您的本地分支,以便您可以继续工作。

default="local-dev-whatever-the-name-of-your-local-branch"
read -p "Enter local branch [$default]: " local
local=${local:-$default}
echo "Local branch is $local"

if [ -z "$local" ]
then
bin/git-merge.sh
else
    printf "Enter remote branch: "
    read remote

    if [ -z "$remote" ]
    then
        printf "Cannot continue without remote branch!\n\n"
        exit
    fi

    git add .
    git add -u
    read -r -p 'Commit description: ' desc

    if [ -z "$desc" ]
    then
        printf "\nExit: commit description is empty!"
    fi

    git commit -m "$desc"
    git checkout $remote
    git status
    git merge $local
    git push
    git status
    git checkout $local
    git status
    printf "\nEnd local commit on $local; merge and push to branch $remote. Well done!\n"
fi

-1
这里有一个脚本,可以提交并推送您在开发中所做的更改,并带有格式良好的提交信息。 提交信息的格式如下:
<Author Name> #first Line that script asks to enter from user
- Git Commit message -- # Second Line that script asks to enter from user

-List of added/Modified files

https://github.com/AdityaSingh0/gitFormatedCommitScript/blob/main/CommitGit


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接