Dev tools and workflow · Intermediate · 18 min
Manage your dotfiles with Git and GNU Stow
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · GNU Stow 2.4.1 · Git 2.53 (Ubuntu packages)
You will keep your shell, Git, tmux and editor settings in one Git repository, linked into place with GNU Stow, with private settings in untracked local files and a bootstrap script that rebuilds the setup on a new account.
On this page
Your settings live in a handful of hidden files in your home folder: ~/.zshrc, ~/.gitconfig, ~/.tmux.conf and an editor config. They build up over years, one alias and one option at a time, and they are lost the day a laptop dies or you start on a new server. Keeping them in a Git repository fixes that, but programs expect their files in fixed places, not inside a repository.
GNU Stow closes the gap. You keep the real files in ~/dotfiles-demo/, one folder per program, and Stow creates symbolic links in your home folder that point into it. Programs read the links as if they were the files, and Git tracks the folder. Stow is a small Perl program from the Ubuntu archive, so the whole setup costs a few hundred kilobytes to install on a new machine, which helps on a metered or prepaid connection.
By the end you will have:
~/dotfiles-demo/withzsh,git,tmuxandnvimfolders, under Git- your existing files adopted into it, after a backup
- secrets and machine-specific settings in
*.localfiles that are never committed - a
bootstrap.shthat links everything on a fresh account and is safe to run again
This tutorial changes real files in your home folder. Step 2 backs them up first, and the last step shows how to go back.
Step 1 · Install Stow, Git, zsh and tmux
On Ubuntu, install the four tools from the archive:
sudo apt update
sudo apt install -y stow git zsh tmuxYou do not need Neovim installed to follow along; its config is a plain file, and the same approach works for any editor that reads a file from your home folder.
Check it worked
stow --version
git --versionThe first line says stow (GNU Stow) version 2.4.1 and the second something like git version 2.53.0. On a Mac, brew install stow tmux gives you the same Stow version and the tmux used later; Git comes with the Xcode Command Line Tools or brew install git.
Step 2 · Back up the files you already have
Warning: later steps replace ~/.zshrc, ~/.gitconfig, ~/.tmux.conf and ~/.config/nvim with links. Copy whatever you have now into ~/dotfiles-backup before anything else. The command refuses to run a second time, so a later run cannot overwrite a good backup with links:
if [ -e ~/dotfiles-backup ]; then
echo "~/dotfiles-backup already exists. Leaving it alone."
else
mkdir ~/dotfiles-backup
for f in .zshrc .gitconfig .tmux.conf .config/nvim; do
if [ -e ~/"$f" ]; then cp -a ~/"$f" ~/dotfiles-backup/; fi
done
ficp -a keeps permissions and timestamps. Files you do not have are skipped.
Check it worked
ls -la ~/dotfiles-backupYou see a copy of each config file you had, for example .gitconfig from when you set up Git. An empty folder is fine too: it means there was nothing to lose.
Step 3 · Lay out the repository, one folder per program
Stow calls each top-level folder in the repository a package. Inside a package, files sit at the path they should have relative to your home folder. So zsh/.zshrc becomes ~/.zshrc, and nvim/.config/nvim/init.lua becomes ~/.config/nvim/init.lua.
Create the folders:
mkdir -p ~/dotfiles-demo/zsh ~/dotfiles-demo/git ~/dotfiles-demo/tmux ~/dotfiles-demo/nvim/.config/nvim
cd ~/dotfiles-demoWrite a small starter for each program. If you already have your own version of a file, write the starter anyway; Step 5 swaps in yours and lets you compare.
cat > zsh/.zshrc <<'EOF'
# ~/.zshrc, managed in ~/dotfiles-demo
HISTFILE=~/.zsh_history
HISTSIZE=50000
SAVEHIST=50000
setopt share_history hist_ignore_all_dups
alias ll='ls -lah'
alias dots='cd ~/dotfiles-demo'
EOF
cat > git/.gitconfig <<'EOF'
[init]
defaultBranch = main
[pull]
ff = only
[alias]
lg = log --oneline --graph --decorate
EOF
cat > tmux/.tmux.conf <<'EOF'
set -g history-limit 10000
set -g mouse on
set -g base-index 1
EOF
cat > nvim/.config/nvim/init.lua <<'EOF'
vim.opt.number = true
vim.opt.expandtab = true
vim.opt.shiftwidth = 2
EOFAdd a .gitignore at the top of the repository so local files can never be committed by accident, then commit:
cat > .gitignore <<'EOF'
*.local
.DS_Store
EOF
git init -b main
git add .
git commit -m "Add starter dotfiles for zsh, git, tmux and nvim"The .gitignore sits outside every package, so Stow never links it into your home folder. One trap to know about: Stow skips any file called .gitignore inside a package, because it is on Stow's built-in ignore list. If you want a global Git ignore file, keep it at git/.config/git/ignore, which Git reads by default.
Check it worked
git log --oneline
git ls-filesThere is one commit, and git ls-files lists .gitignore, git/.gitconfig, nvim/.config/nvim/init.lua, tmux/.tmux.conf and zsh/.zshrc.
Step 4 · Preview what Stow would do
Stow can show its plan without touching anything. --simulate (or -n) makes no changes, and --verbose prints each link it would make. Stow uses the current folder as the stow directory and its parent, your home folder, as the target, so run it from inside ~/dotfiles-demo.
First create ~/.config if it does not exist. When a target folder is missing, Stow links the whole folder instead of the files in it (it calls this folding). That is fine for ~/.config/nvim, but you do not want ~/.config itself to become a link into one package, because every other program writes there.
mkdir -p ~/.config
cd ~/dotfiles-demo
stow --simulate --verbose zsh git tmux nvim || echo "Stow found conflicts. See the message above."Check it worked
What you see depends on whether any of these files already exist in your home folder:
- None of them exist: one line per link, such as
LINK: .tmux.conf => dotfiles-demo/tmux/.tmux.conf, then a warning that Stow is in simulation mode and did not modify the filesystem. - One or more exist: a list of conflicts, one per existing file, such as
cannot stow dotfiles-demo/git/.gitconfig over existing target .gitconfig since neither a link nor a directory and --adopt not specified, ending withAll operations aborted.Stow checks everything before changing anything, so a conflict means nothing was linked. The next step deals with conflicts.
Step 5 · Adopt your existing files and link everything
--adopt resolves each conflict by moving your existing file into the package, over the starter, and then linking it back. Warning: this overwrites the starter files in ~/dotfiles-demo with your own files. That is why the starters are committed: Git keeps both versions, and your originals are in ~/dotfiles-backup.
cd ~/dotfiles-demo
stow --adopt --verbose zsh git tmux nvim
git status --short
git diffgit status marks each adopted file as modified, and git diff shows how your own file differs from the starter: red lines are the starter, green lines are yours. You now choose, file by file:
- Keep your version: do nothing now; it is committed at the end of the next step.
- Keep the starter: run
git restore <path>, for examplegit restore zsh/.zshrc. Your home folder sees the change at once, because it is a link to this file. The starter.gitconfighas no[user]section, so if you keep it, set your name and email first, either in~/.gitconfig.local(next step:git config --file ~/.gitconfig.local user.name "Your Name"and the same foruser.email) or by adding a[user]block togit/.gitconfig. Otherwise the commit in the next step fails withAuthor identity unknown. - Merge the two: open the file in your editor and copy the parts of the starter you want into your version.
Neovim needs two extra checks, because only init.lua was adopted:
- If your existing config is
~/.config/nvim/init.vim, Stow has now linked the starterinit.luanext to it, and Neovim refuses to start with both (errorE5422, conflicting configs). Either keep Vimscript: from~/dotfiles-demo, runstow --delete nvim(so no link is left pointing at a removed file),git rm nvim/.config/nvim/init.lua,mkdir -p nvim/.config/nvim(git rmalso removes the folders it leaves empty),mv ~/.config/nvim/init.vim nvim/.config/nvim/,git add nvim, thenstow nvim. Or move your settings into the starterinit.luaand delete~/.config/nvim/init.vim. - Any other files in your real
~/.config/nvimfolder, such aslua/orplugin/, are still untracked there. Move them into~/dotfiles-demo/nvim/.config/nvim/, then, from~/dotfiles-demo, rungit add nvimso Git tracks them andstow --restow nvimto link them.
Check it worked
ls -l ~/.zshrc ~/.gitconfig ~/.tmux.conf
realpath ~/.config/nvim/init.luaEach file in the ls output starts with l and ends with an arrow such as .zshrc -> dotfiles-demo/zsh/.zshrc. Stow writes relative links, so the setup keeps working if your home folder is mounted somewhere else. realpath prints a path inside ~/dotfiles-demo/nvim/.
Step 6 · Keep secrets and machine settings in local files
Some settings must not go into the repository: API tokens, a work email address, a proxy that only exists on one network. Each config includes a local file at the end, so the local file can add to or override what is shared. The lines below are appended only if they are missing, so this is safe to run again, and it works whether you kept your own files or the starters:
cd ~/dotfiles-demo
grep -q 'zshrc.local' zsh/.zshrc || cat >> zsh/.zshrc <<'EOF'
# Machine-specific and secret settings. Not in Git.
if [[ -r ~/.zshrc.local ]]; then source ~/.zshrc.local; fi
EOF
grep -q 'gitconfig.local' git/.gitconfig || cat >> git/.gitconfig <<'EOF'
# Machine-specific settings, such as a work email. Not in Git.
[include]
path = ~/.gitconfig.local
EOF
grep -q 'tmux.conf.local' tmux/.tmux.conf || cat >> tmux/.tmux.conf <<'EOF'
# Machine-specific settings. Not in Git.
source-file -q ~/.tmux.conf.local
EOFGit and tmux ignore a missing include (-q tells tmux to stay quiet about it), and the zsh line checks first, so a machine without local files still works. Each include is the last line of its file, so local settings win.
Now create the local files. Warning: install would replace an existing file, so each command only runs if the file is missing. -m 600 makes each one readable by you alone from the moment it exists:
for f in .zshrc.local .gitconfig.local .tmux.conf.local; do
if [ ! -e ~/"$f" ]; then install -m 600 /dev/null ~/"$f"; fi
done
echo 'export DOTFILES_MACHINE="laptop"' >> ~/.zshrc.local
git config --file ~/.gitconfig.local credential.helper "cache --timeout=3600"The credential cache is an example of a per-machine choice: fine on your own laptop, not on a shared server. On a work laptop, this is also where git config --file ~/.gitconfig.local user.email sets your work address while the shared config keeps your personal one.
Put tokens in ~/.zshrc.local with an editor, for example nano ~/.zshrc.local, and add lines such as export SOME_API_TOKEN= followed by the value. Do not type them into an echo command, which leaves them in your shell history. For keeping secrets out of every other repository too, see keep secrets out of Git.
Git writes through the links, so git config --global keeps working and the change lands in the repository. Add an alias and commit everything from this step and the last:
git config --global alias.st "status --short --branch"
ls -l ~/.gitconfig
git add -A
git diff --cached --quiet || git commit -m "Adopt existing settings and include local files"git add -A stages every change in the repository, including files you moved in for Neovim; the *.local rule keeps local files out. git diff --cached --quiet succeeds when nothing is staged, so the commit only runs when there is something new.
Check it worked
zsh -i -c 'echo "machine: $DOTFILES_MACHINE"'
git config --show-origin credential.helper
tmux new-session -d -s dotfiles-check
tmux show-options -g history-limit
tmux kill-server
cd ~/dotfiles-demo
git status --short
git check-ignore -v zsh/.zshrc.localzsh prints machine: laptop. Git shows the helper coming from file: followed by the path of your .gitconfig.local. tmux prints history-limit 10000, or your own value if you kept your file. ls -l ~/.gitconfig before the commit still showed a link. git status --short prints nothing, and git check-ignore reports that .gitignore rule *.local would ignore a local file even if one ended up in the repository.
Step 7 · Write a bootstrap script that is safe to run twice
On a new account you want one command that links everything. Create bootstrap.sh at the top of the repository:
cat > ~/dotfiles-demo/bootstrap.sh <<'EOF'
#!/bin/sh
# Link the dotfiles in this repository into $HOME. Safe to run more than once.
set -eu
DOTFILES_DIR=$(cd "$(dirname "$0")" && pwd -P)
PACKAGES="zsh git tmux nvim"
if ! command -v stow >/dev/null 2>&1; then
echo "GNU Stow is not installed. On Ubuntu: sudo apt install stow" >&2
exit 1
fi
# Real folders that programs share, so Stow links files inside them
mkdir -p "$HOME/.config"
# Empty, private local files, created only if missing
for f in .zshrc.local .gitconfig.local .tmux.conf.local; do
if [ ! -e "$HOME/$f" ]; then install -m 600 /dev/null "$HOME/$f"; fi
done
# --restow removes and recreates this repository's links, so a second run changes nothing
stow --dir="$DOTFILES_DIR" --target="$HOME" --restow $PACKAGES
if ! git config user.email >/dev/null 2>&1; then
echo "Git has no email yet: git config --file ~/.gitconfig.local user.email you@example.com"
fi
echo "Linked $PACKAGES into $HOME"
EOF
chmod +x ~/dotfiles-demo/bootstrap.shThe script never moves or deletes your files. If a plain file is in the way on a new machine, Stow reports the conflict and changes nothing, set -e stops the script, and you deal with that file (the end of Step 8 shows how) before running it again. --dir and --target are spelled out so it works from any folder.
Run it twice:
~/dotfiles-demo/bootstrap.sh
~/dotfiles-demo/bootstrap.shThen commit it:
cd ~/dotfiles-demo
git add bootstrap.sh
git commit -m "Add bootstrap script"Check it worked
Both runs end with Linked zsh git tmux nvim into and your home folder, and neither prints an error. ls -l ~/.zshrc still shows the same link, and git log --oneline shows three commits.
Step 8 · Rehearse a restore on a fresh home folder
A backup you have never restored is a hope, not a backup. Rehearse in an empty folder that stands in for a new account's home: clone the repository into it, then run the bootstrap with HOME pointing there. Only committed work is in the clone, which is the point: it shows what a new machine would get.
mkdir ~/restore-demo
git clone ~/dotfiles-demo ~/restore-demo/dotfiles-demo
HOME=~/restore-demo ~/restore-demo/dotfiles-demo/bootstrap.shCheck it worked
ls -la ~/restore-demo
realpath ~/restore-demo/.config/nvim/init.lua
HOME=~/restore-demo git config alias.stThe listing shows .zshrc, .gitconfig and .tmux.conf as links into dotfiles-demo/, a real .config folder, and three empty *.local files with -rw------- permissions. realpath prints a path under ~/restore-demo/dotfiles-demo/nvim/, and Git prints status --short --branch, the alias you added in Step 6.
For the real thing, push the repository to a private repository on GitHub or another host (see push to GitHub with SSH). Replace <you> and the repository name with yours:
cd ~/dotfiles-demo
git remote add origin git@github.com:<you>/dotfiles.git
git push -u origin mainOn the new machine, install stow git zsh tmux and set up an SSH key, but do not run git config --global before bootstrapping: it writes a plain ~/.gitconfig, which Stow will refuse to replace. Then:
git clone git@github.com:<you>/dotfiles.git ~/dotfiles-demo
~/dotfiles-demo/bootstrap.shThen fill in the local files on that machine, including your identity: git config --file ~/.gitconfig.local user.name "Your Name" and git config --file ~/.gitconfig.local user.email you@example.com.
If the bootstrap stops on a conflict because ~/.gitconfig (or ~/.zshrc, which some systems create for new accounts) already exists, pick one fix:
- Move what you need into the local file and remove the plain one. For Git, copy the identity across with
git config --file ~/.gitconfig.local user.email "$(git config --file ~/.gitconfig user.email)"and the same foruser.name, thenrm ~/.gitconfigand run the bootstrap again. - Or let Stow take the file and then throw its contents away: from
~/dotfiles-demo, runstow --adopt git, thengit restore git/.gitconfigso the repository version wins. Set your identity in~/.gitconfig.localafterwards, so note your name and email before you start.
If you set up machines over a slow or prepaid connection, a shallow clone (git clone --depth 1) downloads only the latest version of the files.
Step 9 · Remove the rehearsal, and know how to back out
Warning: rm -rf deletes the folder and everything in it. Check the path before you press Enter. It removes only the rehearsal:
rm -rf ~/restore-demoIf you ever want to stop using Stow, --delete (or -D) removes the links it made and leaves the files in the repository alone. Then copy your backed-up files back. Run this only if you want to return to how things were before Step 5; changes you made since then stay in the repository but leave your home folder:
cd ~/dotfiles-demo
stow --verbose --delete zsh git tmux nvim
for f in .zshrc .gitconfig .tmux.conf; do
if [ -e ~/dotfiles-backup/"$f" ]; then cp -a ~/dotfiles-backup/"$f" ~/; fi
done
if [ -e ~/dotfiles-backup/nvim ]; then cp -a ~/dotfiles-backup/nvim ~/.config/; fiTo link everything again later, run ~/dotfiles-demo/bootstrap.sh.
Check it worked
ls -ld ~/restore-demo 2>/dev/null || echo "rehearsal removed"
ls -l ~/.zshrc ~/.gitconfigThe first command prints rehearsal removed. If you backed out, ls -l shows .zshrc and .gitconfig as plain files again (lines start with -, with no arrow), holding what you had before you started. If you had no ~/.zshrc (or no ~/.gitconfig) before Step 2, ls prints No such file or directory for it instead; that is expected, because there was nothing to restore.
Next steps
- Grow your
zshpackage with a config you understand line by line: get more from zsh with history, aliases and completion. - Check the repository and its history for anything secret before you push it anywhere: keep secrets out of Git.
- Add more packages the same way: one folder per program,
stow --simulatefirst,--adoptfor files you already have, then commit. - Install Git on a new machine, then bootstrap before you set your name and email, and put them in
~/.gitconfig.localrather than withgit config --global: install and configure Git.
Related tutorials
- Work on two branches at once with git worktreeYou will fix an urgent bug in a second working tree without stashing your half-finished feature, merge the fix, and clean up every extra tree afterwards.
- Version releases with semantic versioning, Git tags and a changelogYou will take a small shell library from 1.0.0 to 1.1.0 with written version rules, Conventional Commits, a Keep a Changelog file, annotated Git tags, a build stamped by git describe and release notes drafted from the commits since the last tag.
- Search files and shell history interactively with fzfYou will have fzf 0.75 wired into your shell for history search, file insertion and directory jumps, fed by fd with a preview pane, plus two small pickers that switch Git branches and stop processes.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials