Git提交bash脚本

时间:2011-12-13 00:23:43

标签: git bash github

我正在编写一个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 个答案:

答案 0 :(得分:41)

你必须这样做:

git commit -m "$desc"

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

答案 1 :(得分:13)

这是最后两个答案的合并 - 将add -u链接在一起很棒,但嵌入式读取命令给我带来了麻烦。我去了(最后一行用于我的heroku推,如果那是你的方法,改为'git push origin head'):

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

答案 2 :(得分:5)

从索引中删除实际已删除的文件会很有帮助。 git add -u负责此事。此外,您可能需要考虑将这些命令链接在一起,如下所示:

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

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

只是思考的食物(未经检验的食物)。

谢谢!

答案 3 :(得分:3)

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

使用注释作为cmd args调用脚本,减少要推送的键:

$ ./togithub test commit script 

答案 4 :(得分:3)

以下是我用来管理我的git repos的脚本 - 这将包括推送到您的原始分支,您的暂存站点(如果设置)和您的生产站点(如果设置)的选项

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

也许这可以帮助那些希望加快工作流程的人

答案 5 :(得分: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

答案 6 :(得分:0)

这是一个脚本,它使用格式良好的提交消息在开发人员上提交和推送您的更改 提交信息格式如下:

<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