Settings

Theme

Just use `git` to manage your dotfiles

ericgreer.info

26 points by integrii 4 months ago · 14 comments

Reader

rednafi 4 months ago

Sometimes a well-designed, purpose-built tool can replace so many kludgy solutions. This doesn’t solve the problem of reinstating the dotfiles to their original positions in a new machine.

I’ve tried everything from raw git to gnu stow to manual symlinking and everything in between. They all work when the full context of the workflow is fresh in my brain. But after a few weeks I always mess up, even though I’ve documented the process in my dotfiles repo.

I eventually settled on chezmoi, and it just works. I set it up a year ago and haven’t needed anything beyond chezmoi -v diff and chezmoi -v apply.

  • colinb 4 months ago

    I’ve been using Chezmoi. It has lots of niceness. Two things are awkward for me. Maybe you have solutions.

    I know my way around emacs. vimdiff is a foreign country.

    Often my changes are speculative or experimental. So I checkout, edit, apply and only then discover I’ve messed up. Or I can edit in place. Voila vimdiff!

    What’s the better way?

    • twp 4 months ago

      > I know my way around emacs. vimdiff is a foreign country.

      You don't have to use vimdiff. You can use any merge tool you want. See https://www.chezmoi.io/user-guide/tools/merge/.

      > Often my changes are speculative or experimental. So I checkout, edit, apply and only then discover I’ve messed up. Or I can edit in place. Voila vimdiff!

      > What’s the better way?

      There are lots of options. See https://www.chezmoi.io/user-guide/frequently-asked-questions....

      Personally, I tend to use `chezmoi edit` and then use git in my source directory to `git add -p . && git commit` the changes I want to keep and `git checkout` to discard the changes I don't want.

    • rednafi 4 months ago

      Do you run your changes in the original location?

      The canonical workflow is this: you run chezmoi cd and make changes there first. Only then do you run chezmoi apply to propagate the changes to the original location.

      The source of truth is always what lies in the chezmoi directory, not the other way around.

      So if you’re experimenting with the dotfiles in their original location, running chezmoi apply will override them with the snapshot in the chezmoi directory. The vimdiff doesn’t do much here.

      If you want to retain your changes there, you’ll have to manually copy them over to the chezmoi directory. This is a bit awkward, but having a consistent source of truth makes the behavior easier to reason about. I rarely look at the diff.

    • DiabloD3 4 months ago

      I would also like to mention Chezmoi.

      I switched to it after maintaining a custom written POSIX sh script that kept getting further and further out of hand.

      I don't see myself going back to a self-maintained one unless I'm writing it in Rust.

  • integriiOP 4 months ago

    The post does say exactly how to do that. Just init your homedir as a repo, add a remote, and pull. Your dotfiles come right down on top of your new machine's home directory.

onre 4 months ago

Instead of ignoring everything, I've found it better to apply this setting instead:

  git config status.showuntrackedfiles no
faizmokh 4 months ago

I've been using the same method that was viral on HN years ago. Still works well.

https://news.ycombinator.com/item?id=11071754

jadenPete 4 months ago

Git is great, but it doesn’t solve the problem of removing unused dotfiles. For that, I use a custom script that accepts a configuration file mapping each file/directory to a package on my system. Then, when I remove those packages, I’m notified to delete its dotfiles.

I’ve found this helpful for evaluating new packages without cluttering my home directory. It’s especially helpful when using many packages that don’t respect the XDG Base Directory specification; they tend to dump dotfiles directly in my home directory, so keeping it clean is important.

sam_lowry_ 4 months ago

This was discussed a lot on HN.

The problem with this approach is that you can accidentally commit to the $HOME repository.

An approach that avoids that is to use --git-dir= attribute.

E.g. alias dotfiles="git --git-dir=$HOME/.dotfiles"

obeyeater 4 months ago

I use https://github.com/nycksw/ocd which is just the famous StreakyCobra suggestion plus a pre-commit hook to prevent accidentally checking in your whole homedir. There's also a big ignore-file to prevent checking in secrets. I use it daily across several systems. It's intuitive if you use Git a lot. I don't miss my old pattern of farming symlinks.

jmclnx 4 months ago

I usually use RCS for my dotfiles, but I guess git will work OK too.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection