The letter A styled as Alchemists logo. lchemists
Published November 15, 2024 Updated December 12, 2024
Cover
Git Init

Creating a new Git repository might seem trivial but there are a few tricks git init has up it’s sleeve worth talking about. Let’s say we create a new repository like this:

mkdir demo
cd demo
git init
eza --all --group-directories-first --tree .git

The above will yield the following when using Eza to render a tree view of your directory structure:

.
└── .git
   ├── hooks
   │  ├── applypatch-msg.sample
   │  ├── commit-msg.sample
   │  ├── fsmonitor-watchman.sample
   │  ├── post-update.sample
   │  ├── pre-applypatch.sample
   │  ├── pre-commit.sample
   │  ├── pre-merge-commit.sample
   │  ├── pre-push.sample
   │  ├── pre-rebase.sample
   │  ├── pre-receive.sample
   │  ├── prepare-commit-msg.sample
   │  ├── push-to-checkout.sample
   │  ├── sendemail-validate.sample
   │  └── update.sample
   ├── info
   │  └── exclude
   ├── objects
   │  ├── info
   │  └── pack
   ├── refs
   │  ├── heads
   │  └── tags
   ├── config
   ├── description
   └── HEAD

This is great when getting started but as you create more Git repositories, you might desire more customization such as ignoring all of the Git Hook sample scripts and default branch description file. In my case, I never want any of these files since I use my own Git Hooks as defined in my Dotfiles.

To ignore the extra noise, you can supply the --template option to git init with a custom template directory to configure new repositories. Here’s what this looks like when using no templates at all:

rm -rf demo
mkdir demo
cd demo
git init --template /dev/null
eza --all --group-directories-first --tree .git

The above yields the following:

.git
├── objects
│  ├── info
│  └── pack
├── refs
│  ├── heads
│  └── tags
├── config
└── HEAD

The above structure is the bare minimum Git will use to create new repositories when no templates are supplied. If you were following along locally, you might have noticed the this warning: templates not found in /dev/null. To fix, you can add the following to your global Git configuration (this is what I use):

[init]
  templateDir = ~/.config/git/template

💡 If you don’t want to edit your global configuration, you can run this command: git config --global --add init.templateDir ~/.config/git/template.

The above configures Git to use a custom template as located in your XDG configuration. This solves two problems:

  1. Doesn’t produce warnings.

  2. Allows you to create Git repositories with a minimum working file structure.

While I like to use an empty template, that doesn’t mean you can’t customize your template further. For example, maybe you want a README file in each of your newly created repositories. Here’s how you can do that:

rm -rf demo
touch ~/.config/git/template/README
mkdir demo
cd demo
git init
eza --all --group-directories-first --tree .git

The above will yield the following:

.git
├── objects
│  ├── info
│  └── pack
├── refs
│  ├── heads
│  └── tags
├── config
├── HEAD
└── README

Notice the README was added to our newly created repository. From here you can add content, more files, or whatever you need for your own workflow.

If you’d like to dig deeper, make sure to check out the documentation. I highly recommend using this setup especially if you don’t need the sample Git Hooks scripts and/or have your own global scripts. This saves some disk space and keeps your repository clean.

Enjoy!