Creating an Empty Commit

Search for a command to run...

Hi, thank you very much for these tips. I have already found several situations where this can be useful for my project. Now I'm going to launch a small startup, and I have already found a site that will tell me more about the promotion of such projects.
In this series, you can find useful Git commands, configuration and productivity tips.
Every once in a while you might need to move a Git repository while preserving the history. TL;DR Here is how you can move or duplicate a Git repository from an initial repository https://github.com/username/old.git to a new repository https://gith...
This blog post is fourteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 14. This challenge was about optimizing an algorithm that generates exponentially larger and larger strings. I found ...

This blog post is thirteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 13. All solutions are in this GitHub repository. Solving Part One Given a list of x, y coordinates representing dots ...

This blog post is twelfth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described on Day 12. All solutions are in this GitHub repository. Solving Part One Given a set of connections between underground caves, ...

This blog post is eleventh in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 11. This day had many similarities to Day 9, and I solved the challenge using similar methods. All solutions are in thi...

This blog post is the tenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 10. All solutions are in this GitHub repository. Solving Part One Given a set of lines with opening and closing bracke...

There are times when creating an empty commit in Git is useful. A common scenario is the need to trigger a continuous integration pipeline or adding an important commit message that might have some effect. For example, GitVersion integrates with Git and uses commit messages to increase version numbers of your library or application semantically.
This short post shows you how you can create an empty commit.
I needed to create an empty commit when using GitVersion. Let me explain.
GitVersion finds commit messages containing patterns like +semver:minor and determines the corresponding build and release number used in the continuous deployment process. While the best practice is adding this text on the commit with the minor or major change, there are cases where the change is broken up into multiple smaller commits. I personally experienced such a scenario or forgot to add the correct message bumping the version.
So I tried running this:
git commit -m "+semver:minor"
Git, however, did not let me commit, proudly declaring my branch was up to date and there was nothing to commit.
--allow-empty optionGit assumes that creating an empty commit might be a mistake, thus preventing it by default. You can bypass this using the --allow-empty option.
git commit -m "+semver:minor" --allow-empty
With this approach, you can create an empty commit containing no files or any changes, only with your desired message.
--allow-empty-message optionWhen researching to write this post, I stumbled upon the --allow-empty-message option. Similarly to the --allow-empty option, it is not meant for typical workflow. Omitting to write a descriptive short message about what has changed is not good practice and makes it harder to scan through the changes when looking at the commit history.
However, an empty commit with no message might be useful for triggering a Continuous Deployment & Integration (CD/CI) pipeline.
git commit --allow-empty-message --no-edit
This snippet bypasses Git's requirement of adding a message. The --no-edit option keeps the selected empty message and prevents launching an editor, which by default prompts modifying empty commit messages.
In rare cases, you might need to bypass Git's default requirements when making a commit. The --allow-empty and --allow-empty-message options can be useful to create empty commits to trigger some effect. The examples mentioned in this post were triggering a CI/CD pipeline or increasing the semantic version of your package or application.