Skip to content

How to store configs in a git repository

max_ishere edited this page Jun 29, 2022 · 1 revision

Storing configs in a git repo allows you to keep them all in the same place, backed up online, easily synchronizable, and maybe with some extra added features. To set it up you should first create a bare git repo.

git init --bare $HOME/dotfiles

Create a bare repository

What is a bare git repo?

Think of GitHub. git already stores all the information about the files and their history in the .git folder. Why does GH need to store a copy of this information on the server then? Sure you, using the UI, see the files, but they can be generated quite easily and don't require GH to store them on the server. This is precisely what the bare repo is: a directory with an arbitrary name that only contains the content of .git. The working tree can be anywhere and even in multiple places at once!

Why use a bare repo?

  1. It is your $HOME so you store other git repos in there and git doesn't like that.
  2. You won't have any loose .git folders in your $HOME.

Quite handy IMO.

Using a bare repo

Now that we have a bare repo and it doesn't really know where the files are we need to set up a shortcut to interfacing with it.

alias config='/bin/git --git-dir=$HOME/dotfiles/ --work-tree=$HOME'

Alias a long git command for a shorter config

Put this into $HOME/.profile and add source .profile to your preferred .shellrc (.zshrc or .bashrc, etc). BTW you won't be able to use this alias unless you run the same command in the shell, restart your shell or run source .profile.

You can call this alias anything, but now that we have it we can use this alias the same way as a git command. Let's actually do that since we have some more setup to do.

config config --local status.showUntrackedFiles no

Prevents git shatus from considering all the files in your $HOME

At this point, we are done! Following is a simple conclusion of what we have just done.

Summary

# Create a bare git repo in $HOME
git init --bare $HOME/dotfiles

# Create a shortcut for calling git
alias config='/bin/git --git-dir=$HOME/dotfiles/ --work-tree=$HOME'

# Show less files in git status
config config --local status.showUntrackedFiles no

Clone this wiki locally