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

时光毁灭记忆、已成空白 提交于 2019-11-28 23:46:40

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.

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.

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