DEV Community

Cover image for Creating an empty commit in Git
Dzhavat Ushev for This is Learning

Posted on • Updated on • Originally published at dzhavat.github.io

Creating an empty commit in Git

Does this sounds familiar - a Continuous integration (CI) pipeline is stuck and you need to push a new commit to restart it?

So now you’re looking for a place where you can make a dummy change like deleting/adding an empty line, adding/deleting a dot in a comment - any change, so you can make a new commit and push it upstream.

“There must be a way to create empty commits” you tell yourself but you’re too busy looking for a place to make that dummy change instead of searching for a solution to your problem.

Well, there is a better way!

Git allows you to create an empty commit by simply adding the --allow-empty flag to the commig command. Here 👇

git commit -m "chore: empty commit" --allow-empty
Enter fullscreen mode Exit fullscreen mode

Your job is done. 🎉

Thanks for reading. See you in the next post 😎


Thanks to my colleague Morten Hansen for showing me this tip.


Photo by Kate Trysh on Unsplash

Top comments (4)

Collapse
 
tinkermakar profile image
Makar • Edited

It used to be a pain point over a year ago for GitHub Actions, but now they have 2 buttons on the workflows allowing to re-run all test steps or just the failed ones -- sound better to me compared to cluttering the git history with empty commits

Collapse
 
dzhavat profile image
Dzhavat Ushev

Yes, you're right. There's a way to rerun only particular jobs in a pipeline. But I'm not sure whether there's a way to restart a pipeline that is stuck, e.g. it hasn't even started due to some reason (we've had such cases). In this case pushing an arbitrary change forces the pipeline to restart. That's where the empty commit comes into the picture. Instead of making a dummy change just to commit something :)

Collapse
 
tinkermakar profile image
Makar

hmm, very interesting, I have never had such an issue -- that must be pretty annoying

Thread Thread
 
dzhavat profile image
Dzhavat Ushev

Yeah, it is. We have a kind of "master" workflow which runs first, whose purpose is to determine which other workflows should run. When the "master" workflow gets stuck, nothing else runs 😬