Search code examples
gitconfigurationdotfilesrepository-design

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


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?


Solution

  • 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.