Dotfiles and configuration management

Intermediate · 12 min

Keep your shell config in a Git repo and install it on any machine: a symlink script, the bare-repo method, and what GNU Stow adds. Mind the secrets.

Before thisStartup files and login vs interactive shellsHard and symbolic linksAliases and shell functions in rc filesCustomizing the promptTab completion

Josean Martinez · 12 min

Dotfiles and configuration management

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Josean MartinezReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Dotfiles are the hidden configuration files in your home directory (.bashrc, .gitconfig and the like). Keeping them in a Git repository gives you history, a backup and a way to copy your setup to a new machine.
  • The common method is to keep the real files in a repo and symlink them into your home directory. An install script should back up a real file it would replace and be safe to run twice.
  • The bare-repo method puts the Git directory in ~/.cfg with the work tree set to your home, so files are tracked in place with no symlinks. It needs config status.showUntrackedFiles no or git status lists everything in your home.
  • GNU Stow automates the symlink method: a package directory mirrors your home layout, and stow package creates the links. I did not run it, since it was not installed.
  • A dotfiles repo must never hold secrets such as tokens or private keys, and a public one is public. Put per-machine and secret settings in a file that is not tracked and source it.

The problem

You spent a month on your prompt, aliases and completions. Then your laptop dies, or you start on a new machine, or a server needs the same setup. All that work lives in a handful of hidden files scattered across your home directory, and nobody backed them up.

Dotfiles is the name for those files: .bashrc, .bash_profile, .inputrc, .gitconfig and so on. They are plain text, which makes them a natural fit for Git. A repository gives you history, so you can undo a change that broke your shell, and a one-command copy to the next machine. The question is how to make files that must live at ~/.bashrc also be tracked in a repository. Two methods are common. I ran both in a scratch directory used as HOME, so my real files stayed untouched.

Keep the real files in a directory, ~/dotfiles, and make links from where programs look for them. Symbolic links mean the file in ~ is the file in the repo. This is the install script I tested:

#!/usr/bin/env bash
set -eu
src=$(cd "$(dirname "$0")" && pwd)
for f in bashrc gitconfig; do
  target=$HOME/.$f
  if [ -e "$target" ] && [ ! -L "$target" ]; then
    mv "$target" "$target.bak"; echo "backed up $target"
  fi
  ln -sfn "$src/.$f" "$target"
done

I started with a real ~/.gitconfig in place, and no .bashrc, then ran it:

$ dotfiles/install.sh
backed up ~/.gitconfig
$ ls -l .bashrc .gitconfig .gitconfig.bak
lrwxr-xr-x@ .bashrc -> ~/dotfiles/.bashrc
lrwxr-xr-x@ .gitconfig -> ~/dotfiles/.gitconfig
-rw-r--r--@ 23 02:50 .gitconfig.bak

(Columns trimmed.) Two properties matter. A real file is moved aside, never silently overwritten. And a second run is safe, since ln -sfn replaces the link and the -L test skips the backup. I ran it twice and the links were the same. Then git init inside dotfiles and a commit, and you have the whole setup versioned. Commit what you change in ~/dotfiles/, which is the same file.

Method 2: a bare repo with your home as the work tree

The other method skips links. A normal repository has a .git directory inside its work tree. A bare repository is only the Git data, and you can point Git at any directory as the work tree. Making $HOME the work tree means files are tracked where they already sit:

$ git init -q --bare "$HOME/.cfg"
$ config() { git --git-dir="$HOME/.cfg" --work-tree="$HOME" "$@"; }
$ config config --local status.showUntrackedFiles no
$ config add .bashrc && config commit -q -m "add bashrc"
$ config ls-files
.bashrc

config here is a shell function in the style of the aliases lesson. Without the showUntrackedFiles no line, config status would list everything in your home, so that setting is not optional. The method has one sharp edge. On a fresh machine, a plain checkout refuses to overwrite files that already exist:

$ git --git-dir=../newhome/.cfg --work-tree=../newhome checkout
error: The following untracked working tree files would be overwritten by checkout:
	.bashrc
Please move or remove them before you switch branches.
Aborting

That is safe behaviour, but the new machine’s stock .bashrc has to be moved first. For general Git handling, see the Git lessons on configuration and ignoring files.

Where GNU Stow fits

GNU Stow is a symlink farm manager. You lay out a package directory that mirrors your home (bash/.bashrc, git/.gitconfig) and run stow bash git from the parent. It creates the links, and stow -D removes them. It is Method 1 with the script written for you, and the chosen video teaches it. It was not installed on the machine I used, so I did not run it and cannot show output. The idea is the same as the script above: links into a repo.

What not to commit

A dotfiles repo is tempting to push to a public host, and it is easy to leak things:

  • Tokens and API keys that people export from .bashrc.
  • Private keys under ~/.ssh. Never track id_* files.
  • Per-machine paths, work email addresses and hostnames.

Keep the secret and machine-specific parts out. Source an untracked file from your tracked .bashrc if it exists, and add that file to .gitignore:

[ -f ~/.bashrc.local ] && . ~/.bashrc.local

Run git diff --cached before every commit to your dotfiles, the way you would before publishing code, and if a secret does get committed, see the Git lesson on removing secrets from history.

About the video

“How To Easily Manage Your Dotfiles With GNU Stow” is an 11-minute 34-second video from Josean Martinez, published August 2025. Its timestamps cover introduction, installation, setting up a dotfiles directory, using GNU Stow, ignoring files, unstowing and more that was cut off in what I could read. I could not read a transcript. It teaches Stow, which I have not run, and it is a Mac and Linux creator, so check any Homebrew steps against your system. It is an English-language channel. DistroTube’s video on the bare-repo method (2019) is linked as a second view, and it matches Method 2 above.

Where this fits

That finishes the group on working interactively. The next group collects the everyday tools you reach for constantly, beginning with archives with tar and gzip.

Check yourself

  1. 01Why symlink dotfiles from a repo instead of copying them?

    A symlink means the file in your home directory is the file in the repo. Edit one and the other is updated, so you never have to remember to copy changes back before committing.

  2. 02Your install script runs twice. What should be true after the second run?

    Nothing should change or break. Use ln -sfn so existing links are replaced, and only back up a file if it is a real file and not already a symlink.

  3. 03You use the bare-repo method and git status lists hundreds of untracked files. What did you forget?

    git config --local status.showUntrackedFiles no on that repo, so it only reports files you chose to track.

  4. 04Which of these should not go in a public dotfiles repo: .bashrc, .gitconfig, ~/.ssh/id_ed25519, a file of API tokens exported from .bashrc?

    The private key and the token file. Anything secret should stay out of the repo, and bashrc can source an untracked file, such as ~/.bashrc.local, for it.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark