How To Push An Empty Git Commit?
Often, developers encounter situations where they need to trigger a new build without changing the repository's code. Luckily, this is easy to do because Git allows committing empty commits.
To commit an empty commit, use the git commit --allow-empty command.
bashgit commit --allow-empty -m "message"
This article will explore the subject of empty commits in Git and different ways to trigger a new build without changing anything.
Let's get to it 😎.
First of all, let's look at some valid reasons to commit an empty commit.
Why commit an empty commit?
There are multiple reasons why a developer might want to commit an empty commit:
- To re-trigger a new build in the CLI without changing the code.
- To add documentation to the git history.
- Test commands without adding code changes.
Disadvantages
However, before pushing an empty commit, think twice. Your empty commits might add up and pollute the git history in the long run. This may create confusion if you have many developers working on a repository.
How to commit an empty commit?
To commit an empty commit WITH a message, use this command:
bashgit commit --allow-empty -m "Empty test commit"
To commit an empty commit WITHOUT a message, use this command:
bashgit commit --allow-empty --allow-empty-message
After creating an empty commit, push it to the remote server using the git push command.
Note: When pushing a commit, even an empty one, git hooks can trigger.
Is there an alternative to empty commits?
Alternatively, you can use a git tag to trigger a new build.
You can create an annotated tag with a version and a message, like so:
bashgit tag -a v1.0.1 -m "new version"
The build will re-trigger after pushing the tag to the remote, and the git history will stay clean and not polluted.
Final thoughts
In my experience, pushing an empty commit is not the best way to trigger a new build because it pollutes the Git commit history and confuses other developers. I prefer to use Git tags when I need to re-trigger a new build.
Here are some of my other Git tutorials for you to enjoy: