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?
Examples of common dotfiles Most programs store their configurations in your home directory by default.
Git does not track directories; it only tracks files. If there are no files in a directory, that directory does not “exist” to Git when adding or removing files.
Create an empty . gitignore file inside the empty folder that you would like to commit. Git will only track Files and the changes in them. So folders are tracked as part of the file changes in them.
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
~
) to the Git repoYou 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With