2011-12-13 34 views
20

Sto scrivendo uno script bash per aggiungere, commettere, spingere tutti i file in una directory.Git commit bash script

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

Sto ottenendo il seguente errore:

$ ./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 

Non sono sicuro se questo è un problema con la lettura del testo (è echo s bene) o di passarlo a git commit -m.

risposta

29

quello che dovete fare:

git commit -m "$desc" 

Nello script corrente, test sta andando come messaggio di commit e commit e script vengono trattati come prossimi argomenti.

+1

"Fare quotazioni corrette" non può mai essere esagerato abbastanza. Troppi "suggerimenti" e consigli/esempi semi-corretti sulla rete ... –

4

è utile rimuovere dall'indice i file che sono stati effettivamente eliminati. git add -u si prende cura di questo. Inoltre, si può prendere in considerazione il concatenamento questi comandi insieme in questo modo:

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

Se una qualsiasi comando non riesce, si fermerà valutare i restanti comandi.

Solo spunti di riflessione (cibo non testato).

Grazie!

+2

Puoi anche usare '#!/Bin/bash -e' per far uscire lo script se uno dei comandi fallisce. – bluegray

+0

Divertente che questo sia stato upvoted così tanto ... riceverai messaggi di commit vuoti ... –

8

Ecco un'unione delle ultime due risposte: concatenare l'add -u è fantastico, ma il comando di lettura incorporato mi causava problemi. Sono andato con (ultima riga usata per la mia Heroku spinta, passare alla 'git testa origine push' se questo è il vostro metodo):

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

+1 per il comando "&& \" – oak

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

sceneggiatura chiamata con commento come args cmd, meno tasti per spingere:

$ ./togithub test commit script 
2

quello che segue è uno script che uso per rogna miei repo git - questo include la possibilità di spingere al ramo origine, il tuo sito di staging (se l'installazione), e il sito di produzione (se l'installazione)

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

Ho cercato di includere quanti più commenti possibili per aiutarti a capire cosa fa tutto.

fatemi sapere se avete domande.

Inoltre, ho questa messa a punto come questo

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

forse questo può aiutare qualcuno che cerca di accelerare il flusso di lavoro

1

Questo è quello che io uso la maggior parte del tempo di impegnarsi filiale locale e fondersi con filiali remote:

Questo piccolo script di bash consente di aggiungere e impegnarsi su di te r ramo locale, checkout a un altro ramo, unire con esso e spingerlo, e anche checkout di nuovo a il tuo ramo locale, in modo da continuare a lavorare.

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