Play a jingle on Git commits

, posted

When you complete a challenge in a video game, you usually hear a congratulatory sound effect. This is cute and I like it.

What if you could hear a little jingle when you make Git commits?

Step 1: find a sound effect

First, I needed a sound effect that I could play at the command line.

You can get any sound file you want. I downloaded this one from OpenGameArt. If you instead want to play the entirety of “Smooth” by Carlos Santana every time you commit, you can do that and I support you.

(You can download the one I used here.)

I wrote down where I saved this file. For the rest of this post, we’ll pretend I saved it at ~/Desktop/commit_sound_effect.flac.

Step 2: play the sound effect from the command line

Next, I needed to be able to play this sound effect from the command line.

On macOS, I used the preinstalled afplay command.

# Play the sound effect (macOS only)
afplay ~/Desktop/commit_sound_effect.flac

On Linux, I don’t think there’s a preinstalled solution. There are lots of different programs you can install—you might already have one!—but I already have mpv so I just used that.

# Play the sound effect (requires mpv)
mpv ~/Desktop/commit_sound_effect.flac

# Play the sound effect without any output
mpv --really-quiet ~/Desktop/commit_sound_effect.flac

I don’t know how to do this on Windows, but it seems like it can be done.

Once I could play sounds from the command line, it was time to hook it up to Git.

Step 3: add a post-commit Git hook

To quote git help hooks:

Hooks are programs you can place in a hooks directory to trigger actions at certain points in git’s execution.

There are a bunch of different hooks, like “pre-push” or “post-merge”. We’ll use the post-commit hook, because we want to play the sound effect after we make a commit.

We’ll put it in the hooks directory, which is inside your repository’s Git folder at .git/hooks/ by default. For example, it might be at ~/code/my-project/.git/hooks/.

(Git lets you configure the hooks directory with the core.hooksPath config option, if you want to move it around. git config --global core.hooksPath /path/to/global/hooks can be used to set this globally if you want to play a jingle in every repo on your computer…but I don’t recommend this because it overrides any local hooks you might have.)

We’ll create a short shell script inside that directory. Create .git/hooks/post-commit:

We’ll make it executable:

chmod +x .git/hooks/post-commit

Now, when we commit in that repo, a sound will play!