Empty commits in Git

Greg Foster
Greg Foster
Graphite software engineer

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.

  • Git empty commit: To create an empty commit, use the -allow-empty flag with the git commit command: This command creates a new commit with the specified message without changing any files in the repository.
Terminal
git commit --allow-empty -m "Your commit message"
  • 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:
Terminal
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:
Terminal
git commit --allow-empty -m "Initial commit"

For inspiration on initial git commit messages please see this insightful blog post.

  • 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:

Terminal
git push origin <branch-name>
  • 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:
Terminal
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:
Terminal
git push origin <branch-name> --force
  • 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.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2