How to Create an Empty Commit That Has No Changes

There comes a time when all developers find they need to create a commit but don't want to actually change the codebase.

This could be to trigger a new build in the CI pipeline, to retry a flaky test, or simply to increase the number of commits in the contributors tab of the projects Github page. But the point is this is actually a think that you need to do sometimes.

Here's how to do it.


Creating an Empty Commit

A commit that has no changes is called an empty commit.

You create an empty commit by passing the --allow-empty flag with your commit command.

How to Learn AWS
How to Learn AWS
Learn AWS the easy way, become a leader on your team.
How to Learn AWS
LEARN MORE

This might look like this:

git commit --allow-empty

Or if you prefer to type your commit messages in the CLI too, it would resemble this:

git commit --allow-empty -m "Retry the test"

What Happens Without the --allow-empty flag?

First Git will not create the commit and it will return an error message that looks something like this:

no changes added to commit (use "git add" and/or "git commit -a")

Unless you add changes to the commit or pass along the --allow-empty flag, you won't be able to create your commit.

But I don't like the --allow-empty flag?

Of course you can get around all of this by instead pushing 2 commits.

Making a temporary change in the first commit that you immediately revert in the second.

There are potentially consequences depending on how your team uses commits and how things are setup with your CI pipeline, but this approach avoids having to remember this flag.

Wrapping Up

I hope this helped and was able to solve your problem.

A couple last minute callouts I want to include too. Pushing an empty commit just to retry a brittle or flaky test is generally not a good idea. It would be a better long-term investment to address the root cause and fix the test. That way other environments like your staging or production builds don't fail on the same flaky test.

Happy coding!

Featured
Level up faster
Hey, I'm Nick Dill.

I help people become better software developers with daily tips, tricks, and advice.