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

微笑、不失礼 提交于 2019-12-18 02:48:36

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/31841737/how-do-i-track-dot-configuration-files-in-my-home-directory-with-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!