47

I'm writing a bash script to add, commit, push all files in a directory.

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

I'm getting the following error:

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

I'm not sure if this is a problem with reading in the text (it echos fine) or passing it to git commit -m.

7 Answers 7

60

You have to do:

git commit -m "$desc"

In the current script, test is going as commit message and commit and script are being treated as next arguments.

2
  • 3
    "Do proper quoting" can never be overstated enough. Way too many subpar “howtos” and halfcorrect advice/examples on the net...
    – jørgensen
    Dec 13, 2011 at 1:55
  • 100% - if you're passing the entire command as an argument you can backslash the quotes i.e. ./myscript.sh git commit -m \"my commit message\"
    – chuckwired
    Jan 25, 2021 at 16:10
20

Here's a merge of the last two answers - chaining together the add -u is awesome, but the embedded read command was causing me troubles. I went with (last line used for my heroku push, change to 'git push origin head' if that's your method):

#!/bin/bash
read -p "Commit description: " desc
git add . && \
git add -u && \
git commit -m "$desc" && \
git push heroku master
1
  • Just do git add -A . instead of git add . && git add -u Apr 3, 2019 at 14:19
7

it is helpful to remove from the index the files that have actually been deleted. git add -u takes care of this. Also, you may want to consider chaining these commands together like this:

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

If any command fails, it will stop evaluating the remaining commands.

Just food for thought (untested food).

Thanks!

2
  • 3
    You can also use #!/bin/bash -e to make the script exit if any of the commands fail.
    – bluegray
    Apr 5, 2012 at 14:21
  • 3
    Funny that this got upvoted so much… you'll get empty commit messages… Sep 19, 2016 at 12:12
6
#!/bin/bash  
git pull
git add .
git commit -m "$*"
git push

call script with comment as cmd args, less keys to push:

$ ./togithub test commit script 
5

The following is a script that I use to mange my git repos - this will include the option to push to your origin branch, your staging site ( if setup ), and your production site ( if setup )

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

I tried to include as many comments as possible to help you understand what everything does.

let me know if you have any questions.

also, i have this setup like this

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

maybe this can help someone looking to accelerate workflow

2

This is what i use most of the time to commit local branch and merge with remote branches:

This little bash script allows you to add and commit on your local branch, checkout to another branch, merge with it and push it, and also checkout again to your local branch, so that you continue to work.

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
-1

here is a script that commit and push your changes on dev with a well formatted commit msg Format of commit message is as follows:

<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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.