Mohamed El-Zohairy

A Step-by-Step Guide to Using Bare Git Repositories to Manage Dotfiles

This approach was first described by StreakyCobra on Hacker News. It uses a bare Git repository in the user’s home directory to manage and version control dotfiles directly without needing symbolic links or a separate directory for dotfiles.

Here’s a step-by-step explanation of the process:

Step 1: Create a bare Git repository

Run git init --bare $HOME/.myconf in the Terminal. This command initializes a new bare Git repository at ~/.myconf. A bare repository does not have a working tree, which means it only contains the Git history and not the actual files.

Step 2: Set up an alias for the Git command

Run alias config='/usr/bin/git --git-dir=$HOME/.myconf/ --work-tree=$HOME' in the Terminal. This command creates an alias called config that maps to the Git command with the specified --git-dir (the location of the Git repository) and --work-tree (the location of the actual files) options. By using the config alias, you can run Git commands directly on files within your home directory.

Step 3: Configure the repository

Run config config status.showUntrackedFiles no in the Terminal. This command tells Git not to show untracked files when running config status. This is useful to avoid clutter in the status output since many files in your home directory may not be relevant to your configuration.

Step 4: Use the config alias to manage your dotfiles

Now you can use the config alias like you would use the git command to manage your dotfiles:

This approach lets you manage and version control your dotfiles directly within your home directory without needing to create symbolic links or manage a separate dotfiles directory.

Step 6: Get the dotfiles on a different machine

To replicate this setup on a new machine, you can clone the bare repository into a new .myconf directory and set up the config alias :

git clone --bare <remote-repo-url> $HOME/.myconf
alias config='/usr/bin/git --git-dir=$HOME/.myconf/ --work-tree=$HOME'
config config status.showUntrackedFiles no
config checkout

If you encounter any issues with the checkout step, such as conflicts with existing files, you can backup the conflicting files, delete them, and run config checkout again.