NerdOnTour
NerdOnTour

Reputation: 734

Backticks in git commit message

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?

Upvotes: 19

Views: 4551

Answers (3)

Júlio Tati
Júlio Tati

Reputation: 21

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.

Upvotes: -1

Andreas Louv
Andreas Louv

Reputation: 47099

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.

Upvotes: -1

Andreas Louv
Andreas Louv

Reputation: 47099

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.

Upvotes: 36

Related Questions