Create an Empty Commit

Beginner

This tutorial is from open-source community. Access the source code

Introduction

In Git, a commit is a snapshot of the changes made to a repository. Each commit has a message that describes the changes made. Sometimes, you may need to create an empty commit with no changes to serve as a placeholder or to trigger a build process. In this lab, you will learn how to create an empty commit in Git.

Create an Empty Commit

You need to create an empty commit in your Git repository. This can be useful in several scenarios, such as:

  • Triggering a build process
  • Creating a placeholder commit
  • Marking a specific point in the repository's history

For this lab, let's use the repository from https://github.com/labex-labs/git-playground:

  1. Clone the repository to your local machine using the command git clone https://github.com/labex-labs/git-playground.
  2. Navigate to the repository's directory using the command cd git-playground and configure your github account in the environment using commands git config --global user.name "your-uername" and git config --global user.email "your-email".
  3. Use the command git commit --allow-empty -m "Empty commit" to create an empty commit with the message "Empty commit".
  4. Verify that the empty commit was created by using the command git log --name-status HEAD^..HEAD.

This is where you run git log --name-status HEAD^..HEAD and the result:

git log empty commit result

Summary

Creating an empty commit in Git is a simple process that can be useful in several scenarios. Use the git commit --allow-empty -m <message> command to create an empty commit with the provided message.