How do I track "dot" configuration files in my home directory with git?
Asked Answered
W

2

12

I have a number of "dot" files in my home directory that I'd like to track with git - e.g. .pryrc, .zshrc, etc. I want to have a remote repository for these files so that a) there is an easy way to recover my configuration settings should I lose my machine for any reason; and b) to safely track any changes made to the files in the event I screw something up when configuring changes.

I initially set up a git repository in the home directory to track them, with a .gitignore file configured to ignore every file except specific, whitelisted file names. However, I realized I'm not crazy about having a git repository in my home directory, and there is also the added distraction of seeing the branch name "master" in my terminal window. I use ZSH with settings to display the git branch in the prompt, and it has turned out to be surprising and confusing to see that I'm in a git repository while navigating in directories that I don't expect to have a repository.

I attempted to create a configuration directory below the home folder, initialize it a git repository, and generate symlinks to the desired files. However, I noted after adding and committing everything that the remote branch only showed the link, not the actual content of the file.

What is the best way to achieve this?

Waistcloth answered 5/8, 2015 at 19:58 Comment(0)
W
8

This is how I figured it out:

First, I created a directory ~/repositories/configurations/ which I use to house the git repository. After navigating to this directory, I run git init as usual.

Next, I created a .gitignore file configured to ignore all files except those on a whitelist. (Note that you can do this later in the process before the commit, but if you run git status before setting up the .gitignore you'll see a LOT of untracked files).

# ignore everything
*

# except these whitelisted files:
!.gitignore
!.pryrc
!.zshrc
!.bashrc
!.bash_profile
!.bash_login

Then point the git repository to a different working directory:

git config core.worktree "/User/username"

Running git status now shows:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  ../../.bash_login
  ../../.bash_profile
  ../../.bashrc
  ../../.gitignore
  ../../.pryrc
  ../../.zshrc

From here, we git add ~/.bash_profile ~/.bashrc ~/.pryrc ~/.zshrc and commit. Setting up and pushing to a remote repository is standard.

One note - I decided that I wanted a local README.md file to document the purpose of the directory and how it was configured, etc. I didn't want this file in my home folder where it would be out of context. After creating the README I had to use -f flag to add it to the repository:

git add -f README.md.

I attempted to add it to the .gitignore whitelist, but couldn't convince git to find the file with variations of the path, such as !~/repositories/configurations/README.md and so forth. Regardless of how I tried to specify the path git didn't see the file. After adding with force though, it's working fine. Someone smarter than me may have a suggestion how to do this.

This procedure worked like a charm. I now have a working repository to track dot file changes and safely store them remotely.

Waistcloth answered 5/8, 2015 at 19:58 Comment(1)
Thanks Scro, this works like a charm I find it really useful. I don't have an answer for the .gitignore, went through the same myself.Glabrescent
L
4

Check out Homesick, a framework for keeping track of your dotfiles in a Git repo.

It works by

  • creating a Git repo that keeps track of the files you want it to manage
  • sym-linking the files from your home folder (~) to the Git repo
  • providing commands to track new files, commit/push/pull changes

You can install it by running

gem install homesick

Then follow the documentation to start tracking your files.

I recommend using something like Homesick, as its proven to work, and is minimally invasive. It does not pollute your home folder with a Git repo, and it allows you to share and restore your configuration on any machine you might be working on.

Here's a link to my personal dotfiles repo if you want to take a look at how I'm using Homesick.

Homesick is built using Ruby - if you want a Bash-only solution, check out Homeshick.

For a full list of available solutions around managing your dotfiles, check out the GitHub dotfiles page.

Latialatices answered 6/8, 2015 at 6:29 Comment(4)
Thanks for the recommendation. I read through the documentation. What I don't understand is how this is better than using vanilla git? With the method I outlined, there's no need to rely on an extra gem, with a different API. By setting up a git repository in a logical directory and pointing git to the home directory, I get all the advantages of normal git workflow, without the overhead of managing a different interface.Waistcloth
Fair enough! It's really a "buy vs. build" decision. Homesick (and similar frameworks) have a working solution, and you invested time in creating a custom solution that works for you - nothing wrong with that. Homesick has a command for adding new files (homesick track) while you have to add the file to your .gitignore file - different approaches serving the same purpose. Again, nothing wrong with that, I just wanted to point out an existing alternative.Latialatices
BTW: I've added a link to the GitHub dotfiles page (dotfiles.github.io), which has lots of tools for this purpose. Check out the "General-purpose dotfile utilities" section.Latialatices
Awesome, thanks for doing so! Wasn't aware of Homesick when I tried to figure this out. Good option to know about.Waistcloth

© 2022 - 2024 — McMap. All rights reserved.