How to Create and Push an Empty Commit in Git
Creating an empty commit in Git is a very simple process. All you need to do is add the --allow-empty
flag to the git commit
command. This option lets Git know that you are intentionally creating a commit without any changes.
In the terminal, this is what you will need to type:
$ git commit --allow-empty -m "Your commit message"
If you run git log
afterwards, you will see the newly added commit, which will look like any other regular commit! ✌️
As usual, you can then push it to your remote repository by using the git push command, like in the following example:
$ git push origin main
That's it!
Tip
Empty Commits in Tower
If you are using the Tower Git client, empty commits will be shown, just like any other commit. The difference is that they won't contain any changes, of course!
During merge or rebase operations, if you end up in a scenario where no files were changed and there are no changes to commit, Tower will also allow you to create empty commits.
Why Create Empty Commits?
You might be curious about why someone would want to create an empty commit in Git. After all, aren't commits typically used to keep track of changes to files in a project?
While we recommend that you use them with caution, as they can make the commit history more cluttered, there are actually a few scenarios where this might make sense:
- Triggering CI/CD Pipelines: This is the most common use case for empty commits. Sometimes, you may want to trigger a CI/CD pipeline without actually making any changes to the codebase. Creating an empty commit can be a way to do this, as the pipeline will still be triggered by the new commit.
- Workarounds for Git Hooks: Similar to what we mentioned above, there are situations where Git hooks may need a commit to exist before they can run. You can resolve this by creating an empty commit, which will satisfy this requirement.
- Marking Milestones: Empty commits can be used to mark important milestones or events in a project's history, without actually changing any code. This can help provide context in the commit history.
- Merging Branches: When merging two branches, an empty commit can be created to represent the merge, even if there were no actual conflicts or changes to resolve. This can make the commit history clearer.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Learn More
- More frequently asked questions about Git & version control