1

This question already has an answer here:

I am trying to write a quick bash script to automatically git push to multiple branches at once. My issue is, when I enter my commit message as a read variable, bash and git freak out at the use of whitespace.

Here is my current script:

    #!/bin/bash
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Pushing To all branches"
echo "What is your Commit Message?"
read message

git add *
git commit -m \"$message\"
git push origin
.... (push to other branches) ...

echo "Done!"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

And this works perfectly if my commit message is only one word. However, this is often not the case. For example, if I enter:

Testing test case

as my commit message, I would get the following errors:

error: pathspec 'test"' did not match any file(s) known to git.
error: pathspec 'case"' did not match any file(s) known to git.

Does anyone have any ideas on how to fix this so that my bash script can handle whitespace?

Thanks so much!

marked as duplicate by Jesse_b, Romeo Ninov, G-Man, Jeff Schaller, roaima May 7 '18 at 11:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 5
    You are escaping the quotes around $message causing it to perform word splitting. – Jesse_b May 7 '18 at 2:43
2

Your main issue is calling git commit -m with literal double quotes as part of the message.

The command line given to git will be, if $message is the string some message here,

git commit -m '"some' 'message' 'here"'

This is due to quoting the double quotes.

Instead, this would work:

git commit -m "$message"

Use a commit message template instead.

Assuming that the template is in the file template.txt like so:

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Pushing To all branches
# What is your Commit Message?

... then this would be inserted in the top of the commit message in an editor when you do

git commit -t template.txt

Providing an editor to edit the commit message in would make it much easier for the user to properly format a correct commit message.

You may also set the commit.template in the repository's .git/config file to the pathname of a template file to use (-t is not needed then).

Any line starting with # in the template will not be committed.

See also:

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