Create Git repo with an empty commit

I create a fair number of Git repositories (repos) on my computer. They don’t always get published on GitHub. I remember a while ago, developers on Twitter talking about starting git repos with an empty commit, but I was occupied by something else at the time. I don’t remember what that was now though. I wanted to know the reason why people thought it was a good idea to start a repo with an empty commit. FYI, The answer is so you can use rebase safely. So I did some web searching. I found this helpful article by Larry Garfield.

Create repo with empty commit

If you’re a fan of my writing, you know I love the command line. It’s a nerdy sort of fun for me, to write a bit of Bash to improve my experience. I improved up the commands Larry used in his post. I just wrapped them in a bash function, and added a bit of logic.

function git_empty_init()
    if [[ -z "$1" ]]; then
        echo "What is your project name?" 
        return 1

    message="Initial ${1} commit"

    git init --initial-branch=main
    git commit --allow-empty -m "${message}"

After you’ve added this to your ~/.bashrc, and opened a new tab in your terminal application, execute the function like so:

$ git_empty_init "Project Name"

There are two features in this function. The first is setting the default branch name to main. In 2020, there was a movement to stop using master as a branch name. The second is accepting the project name as a parameter of my function. I use their project name to fill in the commit message. I can change the structure of the message and still only provide the project name. Perhaps in the future, I’ll add some nice boilerplate text for the body of the commit message. Anyway, I hope this helps.

Sorry, but comments are closed. I hope you enjoyed the article