Sync Dot Files with Chezmoi
- Doc no.
- KKZ-TN-110
- Date
- 2025-10-04
- GMT day
- 2025:277
- Est. read
- 3 min
Keeping dotfiles consistent across multiple machines (macOS, Ubuntu, etc.) is a common challenge. Traditionally, tools like GNU Stow were used to symlink files from a repository into $HOME. Today, chezmoi provides a more powerful, template-driven approach that integrates seamlessly with Git and makes managing dotfiles across systems straightforward.
This post will walk you through:
- Installing chezmoi
- Setting it up on your first machine
- Committing to a remote repository
- Applying and syncing your configuration on other hosts
- Handling OS-specific configuration differences
011. Installing chezmoi#
On macOS (via Homebrew):
brew install chezmoiOn Ubuntu:
sudo apt install chezmoi
# or the installer script
sh -c "$(curl -fsLS get.chezmoi.io)"022. Initializing chezmoi on Your Primary Machine#
Start by letting chezmoi manage your existing dotfiles.
chezmoi initAdd files or directories into chezmoi’s source state:
You can also use chezmoi add to sync changes made to the underlying git repository.chezmoi add ~/.zshrc
chezmoi add ~/.config/fishApply changes to ensure consistency between source and home directory:
chezmoi applywill overwrite files in$HOMEto match the source state.
chezmoi applyCheck what files are managed:
chezmoi managedCheck for differences:
chezmoi diff033. Version Control with Git#
chezmoi stores its source state under ~/.local/share/chezmoi. This directory is just a Git repository.
Initialize and push to GitHub (or any Git host):
chezmoi cd # enter the source directory
git init
git branch -M main
git remote add origin [email protected]:yourname/dotfiles.git
git add .
git commit -m "Initial commit"
git push -u origin mainFrom now on, you can use either raw git commands inside chezmoi cd, or simply prefix with chezmoi git:
chezmoi git status
chezmoi git commit -m "update zshrc"
chezmoi git push044. Setting Up Another Machine#
On a new machine, simply:
brew install chezmoi # macOS
# or
sudo apt install chezmoi # Ubuntu
chezmoi init --apply [email protected]:yourname/dotfiles.gitThis clones your remote repository and applies all dotfiles into $HOME.
When you make changes in the future:
On any machine:
- Edit a file via
chezmoi edit ~/.zshrc(orchezmoi addif you edited directly). - Commit and push with
chezmoi git ….
- Edit a file via
On other machines:
Pull and apply updates with:
bashchezmoi update
055. Handling Machine- or OS-Specific Configuration#
chezmoi uses Go templates (*.tmpl) to allow conditional logic:
[user]
name = "Your Name"
email = "[email protected]"
{{ if eq .chezmoi.os "darwin" }}
[core]
editor = "nano"
{{ else if eq .chezmoi.os "linux" }}
[core]
editor = "vim"
{{ end }}You can also use host-specific files:
dot_zshrc.tmpl
host-macbook/
dot_config/fish/config.fish
host-ubuntu/
dot_config/fish/config.fish066. Useful Commands Cheat Sheet#
Add a file/directory:
bashchezmoi add ~/.config/nvimEdit (opens source file directly):
bashchezmoi edit ~/.zshrcApply to home directory:
bashchezmoi applyShow diffs:
bashchezmoi diffPull and apply from remote:
bashchezmoi update
077. Summary#
With chezmoi, your workflow looks like this:
On your primary machine
- Initialize chezmoi
- Add and commit your dotfiles
- Push to a remote Git repository
On any new machine
- Install chezmoi
chezmoi init --apply <git repo>- Run
chezmoi updatewhenever you want to sync
When making changes
- Modify dotfiles via
chezmoi edit - Commit and push from one machine
- Pull updates with
chezmoi updateon others
- Modify dotfiles via
This setup provides a reproducible, Git-backed way of managing dotfiles across macOS, Ubuntu, and beyond, with built-in support for templates, secrets, and host-specific configs.
👉 Do you want me to extend this into a ready-to-publish markdown article (with frontmatter and headings), so you can drop it directly into a blog or static site generator like Hugo/Jekyll?