Red Green Repeat Adventures of a Spec Driven Junkie

git: empty commit message

When working with frameworks, like Ruby on Rails, there’s always the “initial commit” for all the files when commiting the first files for the application.

Umberto Boccioni - Unique Forms of Continuity in Space source

For me, I would like to have this “initial commit” to be the first commit into an empty repository, for example, the folder itself. The first commit is a blank slate and represents the start of the universe.

Let’s see what happens when trying to make an empty commit in a new repository:

vagrant@ubuntu$ git init
Initialized empty Git repository in /home/andrew/empty_commit/.git/
vagrant@ubuntu$ git commit -m 'start of the universe'
On branch master

Initial commit

nothing to commit
vagrant@ubuntu$ git log
fatal: your current branch 'master' does not have any commits yet

Trying to create an empty commit of just the folder did not work… I want to have a commit that contains a message “start of the universe” and has not files or folders.

Documentation to the Rescue

Just looking around to see if there’s a solution, I dove deep into git’s helper flags ($ git commit -h), man pages ($ man git commit), but nothing helped.

A search pointed me to git-scm, one of the best git documentation.

Looking at git-scm’s documentation on git commit page reveals:

–allow-empty Usually recording a commit that has the exact same tree as its sole parent commit is a mistake, and the command prevents you from making such a commit. This option bypasses the safety, and is primarily for use by foreign SCM interface scripts.

Source

git has an option to make an “empty” commit, exactly what I am looking for. Let’s see it working:

vagrant@ubuntu$ git commit --allow-empty -m 'start of the universe'
[master (root-commit) 9962d96] start of the universe
vagrant@ubuntu$ 
vagrant@ubuntu$ git log
commit 9962d967cfc661b9c73427c617f13e1c15e44752 (HEAD -> master)
Author: Andrew Leung <andrew@ualberta.net>
Date:   Thu Apr 18 18:35:46 2019 -0400

    start of the universe
vagrant@ubuntu$

Cool - so now I can have empty commits in git, to indicate the “start of the universe”.

Conclusion

For those situations where an empty commit is necessary, use: $ git commit --allow-empty.

I like to use this when I start a project, I feel like I am “starting the universe”.