Empty Git commits for CI

June 06, 2020 ■ 2 min read

As devs we never get tired of writing our favorite git command everyday once we're done with a certain piece of work.

git commit -m "This should fix it" 😜 
git push origin HEAD  # more on this in next tip 

However, sometimes our work not only involves working with code but also involves working with other tools like CI/CD.

Let's say when working on some feature branch you pushed some changes which breaks CI/CD build. Oops! Maybe it failed because when downloading your dependencies some of the package couldn't be fetched from npm/bintray/pub/jcenter/anything else or you messed up something in config when working on that feature.

Fixing both of those issues has nothing to do with code you've written. You can fix both of those outside code but remember we need to run our CI again with the latest commit on your feature branch once you fix the issue. There are 2 ways to do it.

  • Go to your CI/CD's page of your failed build. There might be nicely looking button allowing you to rebuild the build.
  • Do it directly from terminal 😎

How to do it from terminal?

I'll first tell you the way i've seen people do it which to me feels like an incorrect way of doing it.

Incorrect way:

  1. Add comment or white space somewhere in the code
  2. Commit it
  3. Push it
  4. CI gets re-triggered by listening to those hooks you've setup.

However, there is another way to do it by using built-in commit flag in Git.

Correct way:

git commit --allow-empty -m "Trigger CI" 

That's it. No need to touch any files or add comments just for sake of creating a commit. You can create an empty commit like this by running a single command. Done!

Bonus tip

If you're lazy like me and love setting aliases for every long command you've to type frequently, you can set alias for this command in .bashrc or .zshrc or in config file of whichever shell you're using.

alias gitci="git commit --allow-empty -m 'Trigger CI'"

Then use it:

gitci

And, if you want to use the alias but want an option to write different commit message every time you want to do an empty commit? You can set git alias in .gitconfig file like following.

.gitconfig file:

...
...
[alias]
    ...
    ci = "!f() { git commit --allow-empty -m \"$1\"; return 1;}; f"

Usage:

git ci 'Finally fixed it 😅'

I've been using this for quite a while so decided to share it. Hope that was useful! If you know even better way to do this let me know in comments below.

Want to be notified of similar posts? Follow me @punit__d on Twitter 🙂