gitdotfiles

How to use Git to track user's dotfiles?


As many people do, I have my on GitHub. Having a .git repository on my home directory causes me some trouble sometime.

  1. git status shows me plenty of untracked files.

I can partially solve this by writing a .gitignore that ignore everything that is not a dotfile:

#.gitignore
!.*
.viminfo
.cache
# ...
  1. Everything inside my home directory becomes part of my Git repository

If I create a new directory mkdir foo && cd foo and forget to do git init I will be using my home Git repository instead of the new repository I was supposed to create. So I want to avoid this problem by limiting Git to some folders only.

As I don't usually push/pull my dotfiles, I came across this tick in which I simply rename my home Git repository when I don't use it:

alias githide='mv .git .git_hidden'
alias gitshow='mv .git_hidden .git'

Is there a better alternative against these issues?


Solution

  • I use symlinks to those dotfiles and keep them in a separate folder.

    $ cd ~
    $ ln -s myDotfilesRepo/.gitconfig
    

    If you want to be a bit more advanced you could use GNU Stow to handle dotfiles.

    Let's say your dotfiles repo has the following structure:

    .
    ├── git
    │   ├── .gitconfig
    │   └── .gitignore
    

    Then you could install the whole git package using stow in the following way.

    cd ~
    git clone git@github.com:user/dotfiles.git
    cd dotfiles
    stow git
    

    The result is that both files have been symlinked

    ls -la ~/.git*
    lrwxrwxrwx  1 user user     27 aug.  13 10:17 .gitconfig -> dotfiles/git/.gitconfig
    lrwxrwxrwx  1 user user     27 aug.  13 10:17 .gitignore -> dotfiles/git/.gitignore