2011-12-13 152 views
20

我在寫一個bash腳本來添加,提交,推送目錄中的所有文件。Git commit 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

回答

29

你要做的:

git commit -m "$desc" 

在當前腳本,test正在按提交信息和commitscript被視作下一個參數。

+1

「做適當的引用」永遠不會被誇大。太多的次級「howtos」和半正確的建議/示例在網絡上... –

4

從索引中刪除實際刪除的文件很有幫助。 git add -u照顧這個。另外,您可能需要考慮如下鏈接這些命令:

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

如果有任何命令失敗,它將停止計算其餘命令。

只是想不到的食物(未經測試的食物)。

謝謝!

+2

如果任何命令失敗,您還可以使用'#!/ bin/bash -e'使腳本退出。 – bluegray

+0

有趣的是,這得到了很多...你會得到空提交消息... –

8

下面是最後兩個答案的合併 - 鏈接在一起添加-u是真棒,但嵌入式讀取命令造成我的麻煩。我(用於我的Heroku推最後一行,更改爲「混帳推緣起首」如果這是你的方法)去:

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

「&& \」命令+1 – oak

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

與評論爲CMD ARGS調用腳本,少鍵推:

$ ./togithub test commit script 
2

下面是我使用,以便管理我的git回購協議的腳本 - 這將包括推送到您的起源的分支選項,你的臨時網站(如果設置),同時您的生產現場(如果設置)

#!/usr/bin/env bash 

# die script -- just in case 
die() { echo "[email protected]" 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

也許這可以幫助找人,以加速工作流程

1

這是我用大部分的時間來提交當地分支機構並與遠程分支合併:

這個小bash腳本允許你添加和提交你r本地分支, 結帳到另一個分支,與它合併並推送它,並且再次結帳到您當地的分支 ,以便您繼續工作。

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