Git提交bash脚本

mgo*_*old 32 git bash github

我正在编写一个bash脚本来添加,提交,推送目录中的所有文件.

#!/bin/bash  
git add .  
read -p "Commit description: " desc  
git commit -m $desc  
git push origin master
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

$ ./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
Run Code Online (Sandbox Code Playgroud)

我不确定这是读文本(这echo很好)或传递给它的问题git commit -m.

man*_*lds 44

你必须做:

git commit -m "$desc"
Run Code Online (Sandbox Code Playgroud)

在当前脚本中,test将作为提交消息进行处理,commit并且script被视为下一个参数.

  • "适当引用"永远不会被夸大.在网上太多的subpar"howtos"和半正确的建议/例子...... (2认同)

Jay*_*ler 15

这是最后两个答案的合并 - 将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
Run Code Online (Sandbox Code Playgroud)


god*_*lla 6

#!/bin/bash  
git pull
git add .
git commit -m "$*"
git push
Run Code Online (Sandbox Code Playgroud)

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

$ ./togithub test commit script 
Run Code Online (Sandbox Code Playgroud)


gah*_*ooa 5

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

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

如果任何命令失败,它将停止评估其余命令.

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

谢谢!

  • 如果任何命令失败,您还可以使用`#!/ bin/bash -e`使脚本退出. (2认同)
  • 有趣的是,这得到了如此多的支持……你会得到空的提交消息…… (2认同)

Rob*_*enz 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
Run Code Online (Sandbox Code Playgroud)

我尝试添加尽可能多的注释来帮助您理解所有内容的作用。

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

另外,我有这样的设置

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

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