Skip to content

Git Commit Bang Bang

Posted on:March 31, 2023

What do you use for your first commit message? Something like: git commit -m "First commit"? I do too.

Or I’ll say “initial” instead of “first”. I have git commit -m aliased to gcm so it’ll be something like: gcm "Initial commit".

Well, I wanted to spice it up a little, add a lil pizzazz! So I tried to do: git commit -m "Initial commit!!", with two exclamation marks (bang bang).

But it backfired on me!!

Here’s a screenshot of one of my commit messages on GitHub.

My commit message says, "Initial commitgit add ."

See the outcome? It says: “git commitgit add .”. 🤣

You can see it right here.

Embarrassingly, I didn’t understand why. So, let me see if I can break it down.

The GNU/Linix shell does what’s called expansion, or command expansion. It expands certain characters and substitutes the result of the expansion for the corresponding command.

When you type something like: ls *, the shell expands the * so that it means all or everything, and this expansion happens before the ls command happens, so ls never even sees the star.

And if you know the command line, you know that !! is shorthand for the last command.

So if you run a command, let’s say, apt install some-package and then you get an error message saying that you don’t have permissions to run that command, then you can just do sudo !! to rerun the last command with superuser privileges.

When I did the bang bang (!!) after my commit message, the shell expanded that to mean: rerun the last command, and it turned it into git commit -m "Initial commitgit add ." because my previous command was git add . (which means stage all files in current directory).

In order to fix it, before I learned how to make it work, I had to do git commit --amend and then redo my message.

I just learned this expansion doesn’t happen with single quotes. Single quotes suppress expansion. If I had used single quotes, my bang bang would’ve worked just fine.

I like double quotes though. Maybe I’m weird that way, even in my JavaScript I prefer double quotes and semicolons. So I’m gonna keep using double quotes.

And I learned a little trick to get my bang bang into my commit message with double quotes. You can do: git commit -m "commit message!"! with the second exclamation point outside of the double quotes. And it works!!

Git commit message with two exclamation marks