While a typical Git commit is comprised of a set of code changes, you can also create empty git commits. Creating an empty commit in Git can serve various purposes, such as triggering a build in a CI/CD pipeline, marking a specific milestone in your project, or simply committing without any changes to maintain a commit streak on platforms like GitHub. Here's a comprehensive guide on how to create empty commits in Git, including how to add messages, push them to the remote branch, and use them effectively in your projects.
Creating an empty commit
- Git empty commit: To create an empty commit, use the
-allow-empty
flag with thegit commit
command: This command creates a new commit with the specified message without changing any files in the repository.
git commit --allow-empty -m "Your commit message"
Special use cases
- Git empty commit to trigger build: In CI/CD pipelines, you might want to trigger a build without making any code changes. An empty commit is perfect for this purpose:
git commit --allow-empty -m "Trigger build"
- Git empty initial commit: If you're initializing a new repository and want to start with an empty commit (for example, to establish a branch structure before adding files), you can do so with:
git commit --allow-empty -m "Initial commit"
For inspiration on initial git commit messages please see this insightful blog post.
Handling commit messages and pushing
Git empty commit message: If you mistakenly attempt to create a commit without a message, Git will prevent you from doing so. Git requires every commit to come with an accompanying message. Always include the
m
flag followed by a message.To follow best practices always use conventional commit styling.
Git empty commit push: After creating your empty commit, push it to the remote repository as you would with any other commit:
git push origin <branch-name>
Advanced scenarios
- Git amend empty commit: Amending a commit allows you to modify the last commit. For empty commits, you might want to amend to add a forgotten message or make a minor change:
git commit --allow-empty --amend -m "New message"
- Git force push empty commit: If you need to push an empty commit to a protected branch or override remote changes (use with caution), you can use the force push option:
git push origin <branch-name> --force
Best practices and considerations
Understanding the impact: Before creating empty commits, consider the impact on your project's history and collaborators. Empty commits should have clear, descriptive messages explaining their purpose.
Preventing accidental empty commits: Tools like lint-staged might prevent empty commits to ensure code quality. If your empty commit is intentional, bypass these tools or adjust their configurations as necessary. If you do adjust the configuration of a tool like this however, please remember to change the configuration back once you’re done, or otherwise alert the rest of your team.
Collaboration and communication: When working in a team, communicate the intention behind empty commits, especially if they're used to trigger automated processes or mark specific project milestones. This can be done in the commit message itself.
For further reading please see the official Git documentation on git commits.