Read Anthropic’s case study about Graphite Reviewer

Empty commits in Git

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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"

Alternatively, you can simplify this workflow with Graphite's CLI. Using the gt create command, you can manage commits, branches, and stacks more effectively, enabling streamlined workflows and integrating advanced features like stacked pull requests.

  • 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 (e.g., to establish a branch structure before adding files), you can do so with:
Terminal
git commit --allow-empty -m "Initial commit"

Using Graphite's CLI, you could achieve the same workflow while creating and managing PRs more efficiently with a single command:

Terminal
gt create --message "Initial commit"
  • Git empty commit message: If you mistakenly attempt to create a commit without a message, Git will prevent you from doing so. Always include the -m flag followed by a message.

  • Conventional commit styling: Adhere to conventional commit standards for better clarity.

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

Using Graphite CLI, you can directly manage your push and PR submissions:

Terminal
gt submit
  • Git amend empty commit: Modify the last empty commit's message using the --amend flag:
Terminal
git commit --allow-empty --amend -m "Updated message"

For Graphite users, the equivalent would be:

Terminal
gt modify --commit --message "Updated message"
  • Git force push empty commit: To push an empty commit to a protected branch or override remote changes (use cautiously):
Terminal
git push origin <branch-name> --force
  • Understanding the impact: Use empty commits judiciously to avoid cluttering the project history. Always add clear, descriptive messages.

  • Preventing accidental empty commits: Employ tools like lint-staged to avoid unintentional empty commits. Adjust configurations when necessary, but revert or communicate these changes to your team afterward.

  • Collaboration and communication: Ensure team members understand the purpose of empty commits through detailed messages or documentation. Tools like Graphite can simplify team workflows by organizing PRs, managing feedback, and syncing branches.

For a comprehensive overview of Git commands, visit the official Git documentation or explore Graphite's CLI documentation.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Built for the world's fastest engineering teams, now available for everyone