Escape special character on git commit message

I usually use git commit -m 'message' to commit a message, but sometimes, I need add ' on the message, so I need to escape the single…

Escape special character on git commit message

I usually use git commit -m 'message' to commit a message, but sometimes, I need add ' on the message, so I need to escape the single quote, there are some solutions for me.

For example, I want to commit with message: Fixed the issue that can't get latest data.

Solution 1: use double quote

With double quote is a good solution, we can use the following command:git commit -m "Fixed the issue that can't get latest data."

With this solution we don’t need escape the character.

But if on the message we need double quote, then we can use \ to escape it, for example:git commit -m "This message contain \"."

Solution 2: edit the message with editor

We can just use git commit command, and it will show an editor window, so we can edit any message on it.

Solution3: use single quote

With escape sequence

With single quote, we can use the command below:git commit -m 'Fixed the issue that can'\''t get latest data.'

With $ symbolgit commit -m $'Fixed the issue that can\'t get latest data.'

Explanation

I get help from article Escape a Single Quote in Single Quote String in Bash, from this article, we know that 'a'\''b’ means concatenate three strings together, just like 'a' + \' + 'b'.

Conclusion

For me, I think that the solution 1 is good for me, because I like to use the command line, and I can change my habit to use double quote instead of the single quote.

Resources