git start

10 Dec 2021 11:16 git

When I create a new git repository, I prefer for there to be an empty commit at the beginning of history.

Without one, it becomes difficult to rebase the first proper commit, because there’s nothing to rebase it onto (it has no parent). See, for example, https://www.garfieldtech.com/blog/git-empty-commit.

So:

git init

Because I work on a mixture of git repositories, I don’t set my email globally, so that needs configuring on the new repository before I can do any commits:

email=roger@...
git config user.email "$email"

Once my user name and email are configured correctly, I can make an empty initial commit. I also tag it for later:

git commit --allow-empty -m "Initial commit"
git tag -am "Initial commit" initial-commit

Then I might want to rename the default branch:

git branch -m main

Scripting it

Because I do this fairly frequently, I’ve got a git-start script in $HOME/bin (which is in $PATH). This allows me to run git start and have everything set up correctly.

#!/bin/bash

search_up() {
    f="$1"
    p="$(pwd)"
    while [ "$p" != "/"  ] && [ ! -f "$p/$f" ]; do
        p=$(dirname "$p")
    done

    f="${p%/}/$f"
    echo "$f"
}

default_email="roger@<ellided>"
f=$(search_up ".git-email")
if [ -f "$f" ]
then
    email="$(cat "$f")"
else
    email="$default_email"
fi

git init \
    && git config user.email "$email" \
    && git commit --allow-empty -m "Initial commit" \
    && git tag -am "Initial commit" initial-commit

f=$(search_up ".git-default-branch")
if [ -f "$f" ]
then
    branch="$(cat "$f")"
    git branch -m "$branch"
fi

Then I can just put .git-email and .git-default-branch files in the relevant places under ~/Source, and everything’s sorted.

Related: Using git with multiple emails.