2011-12-13 26 views
20

Estoy escribiendo un script bash para agregar, confirmar y enviar todos los archivos en un directorio.Script Git commit bash

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

estoy consiguiendo el error siguiente:

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

No estoy seguro si esto es un problema con la lectura en el texto (que echo s fina) o pasarla a git commit -m.

Respuesta

29

que tiene que hacer:

git commit -m "$desc" 

En el script actual, test va como mensaje de consignación y commit y script están siendo tratados como argumentos siguientes.

+1

"Hacer citas correctas" nunca se puede exagerar lo suficiente. Demasiados "consejos" y ejemplos/ejemplos medio correctos en la red ... –

4

es útil eliminar del índice los archivos que se han eliminado. git add -u se ocupa de esto. Además, es posible que desee considerar el encadenamiento de estos comandos juntos de esta manera:

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

Si cualquier comando falla, se detendrá la evaluación de los comandos restantes.

Solo alimento para la reflexión (alimentos no probados).

Gracias!

+2

También puede usar '#!/Bin/bash -e' para hacer que el script salga si falla alguno de los comandos. – bluegray

+0

Es curioso que esto haya subido tanto ... recibirá mensajes de confirmación vacíos ... –

8

Aquí hay una combinación de las dos últimas respuestas: encadenar el agregado -u es increíble, pero el comando de lectura incrustado me estaba causando problemas. Fui con (línea usada por última vez para mi empuje heroku, cambie a 'cabeza origen git push' si ese es su método):

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

+1 para el comando "&& \" – oak

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

script de llamada con comentarios como args cmd, menos teclas para empujar:

$ ./togithub test commit script 
2

el siguiente es un script que utilizo para la sarna mis repositorios Git - esto incluirá la opción de empujar a su sucursal origen, su sitio de ensayo (si la instalación), y su sitio de producción (si la instalación)

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

Intenté incluir tantos comentarios como sea posible para ayudarlo a comprender lo que hace todo.

hágamelo saber si usted tiene alguna pregunta.

Además, tengo esta configuración como esta

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

tal vez esto puede ayudar a alguien que busca acelerar el flujo de trabajo

1

Esto es lo que yo uso la mayor parte del tiempo para comprometerse rama local y fusionarse con sucursales remotas:

Esta pequeña secuencia de comandos bash le permite agregar y comprometerse con usted r rama local, pagar a otra sucursal, combinar con ella y empujarla, y también pagar de nuevo a su sucursal local, para que continúe trabajando.

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