Backticks in git commit message
Asked Answered
M

3

19

I did

git commit -m "Changed function name `sum` to `sum_list`"

My intention with the backticks was that sum and sum_list be typed in a monospace font when someone views the commit message in GitHub or the like. It works like this in other contexts, for example in Markdown.
However this didn't work well. A git log shows the following commit message:
Changed function name to

When I googled this, I only found this question about backtick commands, but both the asker and the answerer are already familiar with the concept I am trying to understand.

What do backticks do in commit messages? And is there a way to mark parts of the commit message as monospace font?

Malraux answered 17/2, 2022 at 10:0 Comment(1)
In bash context, command substitution can use either of these syntaxes : <some command> `subcommand` <end of main command> or <some command> $(subcommand) <end of main command>, where the subcommand is executed first, and its output is substituted to the subcommand in the main command. (The backticks syntax is the older one.)Singer
I
34

TLDR: Use single quotes:

$ git commit -m 'Changed function name `sum` to `sum_list`'

Using backticks is a way to tell the shell to execute the content, it's called a command substitution, consider the following:

$ echo "hello `ls` world"
hello Applications
Desktop
Documents
Downloads
Library
Movies
Music
Pictures
Public world

When using single quotes the only special character is another single quote:

$ echo 'hello `ls` world'
hello `ls` world

It's all up for interpretation of the UI on how they will show your git commit message, maybe backticks will render specially in your UI, but consider that the lowest common denominator is git log.

Inessential answered 17/2, 2022 at 10:3 Comment(3)
Very nice, thank you. This also explains why the same problem does not occur when I enter the commit message in a text editor (which opens after git commit). Also, to make this explicit: using backticks is the right way to get a monospace font?Malraux
@Malraux It depends on where you read the commit, as mentioned above, looking at the message with git log will just render the text as is. Reading it in a different commit message viewer, eg on GitHub, might resolve in the message being interpreted as markdown.Inessential
I tried this approach with git commit -m 'Add startup script' -m '...' and got tons of errors like: error: pathspec 'startup' did not match any file(s) known to git error: pathspec 'script'' did not match any file(s) known to git :(Aussie
I
-1

If you set one of the environment variables GIT_EDITOR, VISUAL, EDITOR in your .bashrc or .zshrc file, then you can use:

$ git commit

And your editor will open where you can write the commit message.

Add the following to your .bashrc or .zshrc file.

export GIT_EDITOR='code --wait'

And then on the command line:

$ git commit

will open VS Code, the flag --wait is needed to tell VS Code not to fork, so that git knows that you are done writing the commit message, when you close the editor.

Other commands also respect your EDITOR and VISUAL environment variables:

For instance you can press ^X^E that's control+x followed by control+e, while on the command line. This will open your specified editor with the current command line, and it's now possible to edit it with your editor.

Inessential answered 3/10, 2022 at 16:26 Comment(0)
A
-2

Cool for commit messages, people must always escape the words they want to highlight with backticks.

eg.

  • 🚫 don't do
  • ✅ do
git commit -m "chore: remove unused `code`" 🚫 
git commit -m "chore: remove unused \`code\`" ✅ 
git commit -m 'chore: remove unused \`code\`' ✅ 

Preview

"chore: remove unused code" 🚫
"chore: remove unused `code`" ✅
'chore: remove unused `code`' ✅

The end result will look like the first option, but it should not be written that way without escaping the backticks with \


Another issue i've before is CI/CD tools failing to run a build because of not escaping the backticks in commit messages.

Amoretto answered 5/7, 2023 at 9:21 Comment(1)
This was the answer for me. My comment had don't in it, so I couldn't user single-quotesDietary

© 2022 - 2024 — McMap. All rights reserved.