Skip to main content

How to Manage Dotfiles With Git (Best Practices Explained)

What are dotfiles and why manage them with git?

“Dotfiles” are configuration files for your user environment, typically stored in your home directory (~). They often start with a dot (.) to make them hidden by default in file listings. Examples include .bashrc, .zshrc, .gitconfig, and directories like .config/.

These files control the behavior of your shell, text editors, version control systems, and other applications. Because they define your working environment, having a consistent set of dotfiles across multiple machines can greatly enhance productivity.

Using git to manage your dotfiles has several benefits:

But managing dotfiles with git can be tricky because:

There are two common patterns to solve these challenges. We’ll explore both below.

Two common patterns for managing dotfiles with git

1) “Bare repo” in ~

This method uses some less-known git features to achieve a clean setup. You store the git metadata somewhere not in ~, but tell git that the working tree is ~.

Typical setup:

# First, you clone your repo "bare", i.e. without a working tree
git clone --bare <repo-url> ~/.dotfiles
# Then, define a convenient alias to work with your dotfiles
alias dot='git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
# Now, checkout your files into your home directory. This will probably
# fail the first time if you have conflicting files; move/backup them once.
dot checkout
# Hide untracked files so `dot status` doesn't list literally everything in your home
dot config --local status.showUntrackedFiles no

Pros and cons

Using this method, you keep your dotfiles in a normal git repo directory (e.g., ~/dotfiles) and use a tool to create symlinks from that repo into your home directory. Typically, GNU Stow is used for this, but there are other good tools like chezmoi and yadm.

Repo layout example (Stow style):

dotfiles/
  git/
    .gitconfig
    .config/git/ignore
  zsh/
    .zshrc
    .config/zsh/...
  nvim/
    .config/nvim/...

Deploy with:

cd ~/src/dotfiles
stow git zsh nvim

Or better yet, keep a setup.sh script in the repo that does it for you.

Pros and cons

Which way is “best”?

Both methods are valid and widely used. The choice depends on your preferences:

Personally, I use the second method with GNU Stow. This makes it easier for me to set up new machines: fewer commands to remember, and I can keep setup scripts and documentation in the same repo without issues.