Tracking & Syncing my dotfiles!
Table of Contents
.dotfiles or… (.)²files?
- Using Git + Github, & tracking dotfiles with an alias.
- … as mentioned on the ever-wise Arch Wiki.
# 1. Create a bare Git repo to track dotfiles
git init --bare ~/.dotfiles
# 2. Create an alias to simplify dotfiles management.
# Tells (/usr/bin/git) to link the git alias directory you just created to your real .config/
alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
# 3. Hide untracked files in ~/ from cluttering "git status"
dotfiles config status.showUntrackedFiles no
Setup & communicate with this repo via ssh, authenticating with a local private key.
# 4. Generate SSH key for GitHub auth (if you haven't got one already)
ssh-keygen -t ed25519 -C "you@example.com"
ssh-add ~/.ssh/id_ed25519
# Add the value of ~/.ssh/id_ed25519.pub as an entry in your Github --> Settings --> SSH & GPG Keys, via cat + copy-pasting, or however you'd like.
# 5. Force git on your machine to always push to github with SSH instead of HTTPS
git config --global url."git@github.com:".insteadOf "https://github.com/"
# 6. Set upstream branch as origin main & push via ssh!
dotfiles push --set-upstream origin main
then, upon changing my dotfiles, can push to github with:
dotfiles statusdotfiles add XXXXXdotfiles commit -m "Update shell and Hyprland config"dotfiles push(to remote, via SSH)
- or… using a dotfiles manager, comme chezmoi.
… which is a tool that essentially creates a copy of your dotfiles folder outside of your /home directory (e.g. in ~/.local/share/chezmoi/private_dot_config/) to act as a place to stage, synchronise (with git) & manage changes to your local dotfiles.
I think of it as a remotely-connected playground for your dotfiles, to mess with them, pull them from remote repos etc., before applying the changes (via symlinks, copying, or templating) into your local home directory (e.g. ~/.config).
- To install:
sudo pacman -S chezmoichezmoi init- Check what is & isn’t managed by
chezmoiwithchezmoi managed/chezmoi unmanaged. - … then follow steps on this tutorial to connect to your repository & get your first commit. I’m using
chezmoito push to the same remotedotfilesrepo created above, and so justrebasedmy changes (overwriting the old,chezmoi-lessdotfilesfrom above) to keep it nice and clean.
- Editing your dotfiles & using chezmoi:
You can edit your dotfiles in multiple ways with chezmoi.
(RECOMMENDED) You can work and make changes within the locally-created chezmoi copy of your dotfiles, apply them locally, and push them to remote repo once done.
-
Navigate to your
chezmoidotfiles copy withchezmoi cd(you should be able to tell that it’s thechezmoi-managed copy - e.g. it’s calledprivate_dot_configfor me). -
Then, once you’ve made changes and are ready to see them/apply them to your real
dotfiles(e.g to see changes live made to your desktop GUI), usechezmoi statusto list all changed files,chezmoi diffto check any changes, andchezmoi applyto copy thechezmoi-managed files over to your localdotfiles.Now, you should see any changes made reflected on your live system (after reloading the given services, if applicable) -
Then², once you’re ready to update your remote repo with your changes, go through the usual
git commitprocess within thechezmoi-managed directory. -
git statusto see all changed files (within thechezmoi-managed copy) -
git add .(or whatever files you want to add) -
git commit -m "cool changes -
git push origin main
However, you also have the option of…
…making changes to your dotfiles normally (i.e. not within the chezmoi-managed copy of your dotfiles)
So, after you’re finished a particularly spicy ricing session, you can run:
chezmoi status- to see what’s changed between your localdotfilesandchezmoi’s copy.chezmoi add ~/.config/path/to/file.config- to add any locally-changed files tochezmoi’s tracked &git-managed copy.chezmoi apply -vto write these local changes tochezmoi'sworking copy of yourdotfiles.- Then switch to the
chezmoi-managed copy withcd chezmoi, and go through the usualgit commitprocess to update your remote repo if desired.
chezmoi, importantly, allows you to do some of the following cool things:
- Set up your
dotfileson a new machine with a single command:chezmoi init --apply https://github.com/$GITHUB_USERNAME/dotfiles.git(public repo - private requires other methods) - Using templates to manage
dotfilesbetween different machines/distros. - Encrypting your
dotfilesusing secrets from your password manager