In summary, an empty commit, as the name suggests, is a commit that doesn’t contain any changes to the project’s files. git commit –allow-empty -m “message” command can be used for empty commit.
In this tutorial, we will tell what is an empty commit in Git and how to push an empty commit to GitHub.
Table of Contents
What is an Empty Commit?
An empty commit, also known as a null commit, is a commit that contains no changes to a codebase or files in a repository. It is essentially a commit that has an empty diff associated with it.
Despite containing no changes, an empty commit is a valid commit recorded in the repository’s commit history.
It functions like any other commit with a unique commit hash and metadata like author, date, and commit message.
Why Make an Empty Commit?
There are several reasons why developers intentionally make empty commits on GitHub repositories:
- To trigger rebuild/redeployment pipelines: Many projects have continuous integration/continuous deployment (CI/CD) pipelines configured to run when new commits are pushed. An empty commit can trigger these pipelines and redeploy applications without changing any code.
- To update the last commit date/time: Making an empty commit updates the last updated timestamp of a repository. This can be helpful to indicate a project is still active even if no code changes are ready.
- Placeholder commits: Empty commits can act as placeholders in the commit timeline before meaningful work is ready to commit.
- Correcting corrupted Git history: If the Git history gets corrupted, an empty commit can be used to reset HEAD correctly to fix issues.
Overall, empty commits serve various administrative needs for Git repositories. Mainly in the production environment, they are usually used to trigger builds without changing any code.
How to Push an Empty Commit to GitHub
Pushing an empty commit to GitHub only requires a few Git commands:
1. Create an empty Commit with the option “–allow-empty”:
git commit --allow-empty -m "Empty commit to trigger Build"
2. Push the empty commit to GitHub:
git push origin <branch-name>
After pushing the empty commit to the remote git branch, the empty commit now appears in the repository commit history to serve its intended administrative purpose like triggering CI/CD pipelines.
Developers can push empty commits as needed to manage their repositories.
Example: Triggering Build with Empty Commit
Here is the output of the above commands:
➜ empty-commit git:(main) git commit --allow-empty -m "Empty commit to trigger build"
[main 5ddfd34] Empty commit to trigger build
➜ empty-commit git:(main) git push origin main
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 200 bytes | 200.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/storagetutorials/empty-commit.git
d7a8ffe..5ddfd34 main -> main
➜ empty-commit git:(main)
As you can see in the build screenshot, an empty commit has triggered the CI build. So if you have a CI build that only triggers on new push, an empty commit can be used.
If you would like to try this, you can clone our repo: https://github.com/storagetutorials/empty-commit
Conclusion
In this article on GitHub Empty Commit, we learned what is an empty commit, why it is used, and how to push an empty commit to GitHub with the help of an example. If you have any questions or feedback, please let us know through your comments.