質問

I have some text that needs to be in the commit msg. The text is from our bug tracking system and can't be changed at this moment.

Normally I would do I merge with:

git merge origin/one-419 m'STAGED:ONE-419 - text from ticket'

If the ticket text itself had a single quote. i.e. text isn't blue

I would just surround the message with the other style of quotes, so in this case:

git merge origin/one-419 m"STAGED:ONE-419 - text isn't blue"

However in the case I am looking at there is both single AND double quotes, e.g.

The "text" isn't blue

How can I merge using this message?

I tried

git merge origin/one-419 --no-ff -m'STAGED:ONE-419 - The "text" isn\'t blue'

but I got the continuation prompt so it doesn't appear to be correct.

役に立ちましたか?

解決 3

Some shells, including bash, provide a style of quotes—$'...'—which acts as single quotes, but allows certain escaped characters to be processed specially (including a single quote itself):

git merge origin/one-419 -m $'STAGED:ONE-419 - The "text" isn\'t blue'

A more standard answer is to use double quotes and to quote any embedded double quotes:

git merge origin/one-419 -m "STAGED:ONE-419 - The \"text\" isn't blue"

他のヒント

Use a heredoc:

read string << EOF
STAGED:ONE-419 - "text" isn't blue
EOF

git merge origin/one-419 -m "$string"

What you have is a shell quoting issue rather than a git issue. The way to test these out in general is to use the echo command to see what works.

Some details depend somewhat on which shell you use (the exact quoting mechanisms are different in sh, bash, tcsh, zsh, etc). As you discovered, the rule of thumb is to quote single quotes with double quotes, and vice versa, but this fails when there's nesting.

A demonstration:

$ echo 'STAGED:ONE-419 - The "text" isn\'t blue'
> '
STAGED:ONE-419 - The "text" isn\t blue

$ 

Clearly not quite right. On the other hand:

$ echo 'STAGED:ONE-419 - The "text" isn'"'t blue"
STAGED:ONE-419 - The "text" isn't blue
$ 

The single quote sequence ended at the close single quote, and then I used double quotes to quote the rest of it, including the single quote you wanted carried through.

Programmatically, it may be easier to use double quotes:

$ echo "STAGED:ONE-419 - The \"text\" isn't blue"
STAGED:ONE-419 - The "text" isn't blue
$ 

Double quotes are "less strong" than single quotes, so backslashes work inside them to protect against things double quotes don't protect against: $-expansion, backquotes, backslashes, and more double quotes. This does mean you need to put backslash in front of each such character:

$ echo "this \$works"
this $works
$ echo "but this $doesn't"
but this 't
$ 

so it's a bit clumsier, but easy enough to code in most programming languages.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top