Skip to main content

Tech blog4 min read

Managing Your Dotfiles With chezmoi: A Practical Getting-Started Guide

Pere PagesSoftware Engineer
Dotfiles syncing across two laptops and a phone through a central hub marked with a house-and-keyhole motif

https://www.chezmoi.io/

I've started using chezmoi to manage my dotfiles: shell config, Git config, editor settings, aliases, and all those small files that make a machine feel like mine.

The basic idea is simple:

TEXT
chezmoi source repo ↔ my real $HOME files

But the direction matters.

What chezmoi is

chezmoi is a dotfiles manager. Instead of manually copying .zshrc, .gitconfig, config folders, etc. between machines, you keep a clean source of truth in a Git repo.

By default, chezmoi stores that source here:

ZSH
~/.local/share/chezmoi

That folder is usually a Git repository.

So there are three places to think about:

TEXT
$HOME files # the real files used by the system
~/.local/share/chezmoi # chezmoi source repo
GitHub/GitLab remote repo # remote backup/sync

Initialize chezmoi

ZSH
chezmoi init

This creates the chezmoi source directory:

ZSH
~/.local/share/chezmoi

Then you can check it:

ZSH
chezmoi source-path

Add a file to chezmoi

ZSH
chezmoi add ~/.zshrc

This means:

TEXT
$HOME → chezmoi source repo

For example:

TEXT
~/.zshrc

becomes something like:

TEXT
~/.local/share/chezmoi/dot_zshrc

The weird dot_ name is how chezmoi represents hidden files.

Apply changes

ZSH
chezmoi apply

This means:

TEXT
chezmoi source repo → $HOME

So apply is not a backup command. It restores or syncs the repo version into your real home folder.

Useful when:

  • setting up a new machine
  • restoring your config
  • applying edits made in chezmoi
  • syncing your dotfiles into $HOME

Preview changes before applying

ZSH
chezmoi diff

This shows what would change if you ran:

ZSH
chezmoi apply

Mental model:

TEXT
What does chezmoi want to change in my real $HOME files?

Good habit:

ZSH
chezmoi diff
chezmoi apply

Update chezmoi from files I edited manually

Sometimes I edit the real file directly:

ZSH
~/.zshrc

Then I want chezmoi to learn those changes.

Use:

ZSH
chezmoi re-add ~/.zshrc

This means:

TEXT
$HOME → chezmoi source repo

So:

ZSH
chezmoi apply # repo → home
chezmoi re-add # home → repo

That distinction is the key.

Git is still manual

After chezmoi re-add, Git is not automatically updated remotely.

You still need:

ZSH
cd "$(chezmoi source-path)"
git status
git diff
git add .
git commit -m "Update dotfiles"
git push

So chezmoi manages the mapping between your real files and the source repo.

Git manages history and remote sync.

Check the Git remote

ZSH
git remote -v

This shows where the chezmoi repo is pushed.

Example:

TEXT
origin git@github.com:user/dotfiles.git (fetch)
origin git@github.com:user/dotfiles.git (push)

Stop managing a file

If I want chezmoi to stop tracking a file but keep the real file in $HOME:

ZSH
chezmoi forget ~/.zshrc

Meaning:

TEXT
remove from chezmoi
keep ~/.zshrc

If I want to remove it from chezmoi and delete the real file too:

ZSH
chezmoi destroy ~/.zshrc

Meaning:

TEXT
remove from chezmoi
delete ~/.zshrc

Safer default: use forget.

Useful commands summary

ZSH
chezmoi init

Create the chezmoi source repo.

ZSH
chezmoi source-path

Show where the chezmoi repo lives.

ZSH
chezmoi add ~/.zshrc

Start managing a file.

ZSH
chezmoi re-add ~/.zshrc

Update chezmoi with changes made in the real home file.

ZSH
chezmoi diff

Preview what apply would change.

ZSH
chezmoi apply

Copy chezmoi source files into $HOME.

ZSH
chezmoi managed

List files managed by chezmoi.

ZSH
chezmoi forget ~/.zshrc

Stop managing a file, but keep it in $HOME.

ZSH
chezmoi destroy ~/.zshrc

Stop managing a file and delete it from $HOME.

My mental model

The important thing is direction:

TEXT
chezmoi add = home → repo
chezmoi re-add = home → repo
chezmoi apply = repo → home
chezmoi diff = preview repo → home
chezmoi forget = remove from repo, keep home file
chezmoi destroy = remove from repo, delete home file

And after changing the chezmoi repo:

ZSH
cd "$(chezmoi source-path)"
git add .
git commit -m "Update dotfiles"
git push

That's the whole workflow: edit, capture, commit, push, and later restore with apply.

Chezmoi Logo

References

  • chezmoi.io — official documentation, including the quick start, command reference, and user guide

Found it useful? Pass it on.

XLinkedIn

Spotted a mistake? Suggest an edit on GitHub