How to Push an Empty Commit in Git?

Learn via video courses
Topics Covered

In Git, a commit represents a snapshot of your repository at a specific point in time. An empty commit contains no changes, simply marking a moment or action in your project's history. While Git typically requires changes in the local repository for committing, there are instances where pushing an empty commit is necessary, such as for documentation, milestones, or triggering Git hooks.

The standard Git commit command uses the -m option to include commit messages directly from the command line, avoiding text editor prompts:

To push an empty commit, modify this command by adding the --allow-empty flag. For example:

This creates a new commit object with no code changes. After creating an empty commit, you can push it to the main branch using:

Such commits are useful in situations like marking the release of a new version or running automated tests via Git hooks. Overall, empty commits are a versatile tool in Git for adding non-code-based information to your project's timeline.

Pre-Requisites

Before delving into the concept of pushing a git empty commit to a remote repository, you should understand the concept of a commit in Git. A commit is one of the most commonly used concepts in Git and a fundamental step in a Git workflow. It is used to create a snapshot of the state of a repository at a particular point in time, which helps you to track and view the history of your Git repository up to the point when that particular commit was made.

A commit can be identified by a unique commit ID. You can also upload a message for your commit and this will show the details of the committed code changes. Along with this, a commit consists of a lot of additional data, such as the author's name and the timestamp when the commit was created.

Need to Push an Empty Commit

Sometimes you need to start a new build without changing anything in your code locally. Or you might not have an option or access to start the build manually. In this case, the only way to start a build is to push an empty commit with Git.

That's where the concept of pushing a git empty commit comes into play. If you can't initiate the build manually, pushing an empty commit from the terminal can lead to the starting of the build without changing anything in the code ` locally. However, until and unless it is not much needed, we should develop the habit of writing commit messages as it helps other team members to understand the context of the commit and the reason why that commit exists in history.

To summarize, there are two very good reasons why you might be running an empty commit:

  1. First is when a build step should be triggered.
  2. Second is when you start a new branch of features.

In the first case, imagine if you just need to trigger a new build, but without having any change in your codebase. Empty commits are much better candidates in this case than making small changes to files just to allow commits.

For the second case, imagine you are working on a fairly large feature and you’ll be working on this feature for a few days. For this, you will be making several commits which you’ll then combine into a single commit using the git rebase command. It would be nice to start with an empty commit with a simple name, create a new branch, and then later use that as a starting point for rebasing.

How to Push an Empty Commit?

You can push an empty commit with the Git commit -m command along with using the --allow-empty flag:

It's very similar to pushing commits when you make changes to your code, except that you add the --allow-empty flag. However, this flag allows you to push commits without making any code changes.

The reason why we added this flag is to make it clear that this is an empty commit and the next step is to push this commit to the root directory and for that, you can use the below command:

After running the above command, you can see that the commit has been pushed to the branch without any changes.

Conclusion

  • A commit in Git is the concept of taking a snapshot of the repository at a particular point in time and as a developer, you may need to push many commits to your Git repository after making your code changes.
  • A commit consists of a lot of additional data, such as the author's name and the timestamp when the commit was created.
  • git commit -m "Commit message here" command will perform a commit with the specified commit message.
  • Sometimes you need to start a new build without changing anything in your code locally. Or you might not have an option or access to start the build manually. In this case, the only way to start a build is to push an empty commit with Git.
  • If you can't initiate the build manually, pushing an empty commit from the terminal can lead to the starting of the build without changing anything in the code locally.
  • An empty commit is a commit with no changes.
  • When you want to trigger a new build, but without having any change in your codebase. Empty commits are much better candidates in this case than making small changes to files just to allow commits.
  • You can push git empty commit with the -m command along with using the --allow-empty flag.
  • git commit --allow-empty -m "Empty commit is used to make an empty Git commit.
  • git push origin main command is used to push the commit into the root directory.
  • When you are working on a fairly large feature, it would be nice to start with an empty commit with a simple name, mark a new branch, and then later use that as a starting point for rebasing or combining multiple commits into a single commit.
  • The command git commit --allow-empty -m "Empty commit" is very similar to pushing commits when you make changes to your code, except that you add the --allow-empty flag. However, this flag allows you to push commits without making any code changes.