I decided to host some of my configuration files on github, but I ran into a problem getting it set up on multiple computers. Git doesn't let you clone into a non-empty directory. This makes sense 99% of the time but it's annoying in this case. I considered just creating the repo in ~/dotfiles and using some symlink magic with a script to handle it, but I think this solution is better.

Here are the steps I settled on after trying a few different methods:

cd  
git init  
git remote add origin git@github.com:myusername/myawesomerepo.git  
git fetch  
git branch master origin/master  
git reset --hard origin/master  

If you include your .vim directory in your repo, add a .gitignore in that directory with .netrwhist and .VimballRecord to prevent any privacy issues.

Bonus: My zsh prompt shows if the current repository is clean or dirty, but since there are always going to be untracked files in my home directory it would always show that the repo was dirty. To fix this you can create a .gitignore file in your home directory with the single entry "*" in it. This will ignore everything except what is already being tracked. This has the added benefit of preventing you from accidentally adding something that you don't want tracked because you will have to git add --force anything new into the repo.

Update: Because a git repository includes any subdirectories of the repo root, and my home directory is a git repository, my terminal prompt would always indicate that I was in a git repo. This got to be annoying, so I went looking for a solution. I found this post by Brandon Rhodes, and really liked the idea so I added this modified version of his script to my ~/bin directory:

#!/usr/bin/env bash
# ~/bin/home
# toggles source control on my home directory
# idea from [http://rhodesmill.org/brandon/2012/home-directory-vc/]

# colorized output
function cinfo() {  
  echo -e "\x1b[32m$1\x1b[0m" # green
}
function cwarn() {  
  echo -e "\x1b[33m$1\x1b[0m" # yellow
}
function cerror() {  
  echo -e "\x1b[31m$1\x1b[0m" # red
}

if [ -d $HOME/.git.off ]; then  
  mv $HOME/.git.off ~/.git || exit 1
  cinfo "home directory version control activated"
elif [ -d $HOME/.git ]; then  
  mv $HOME/.git $HOME/.git.off || exit 1
  cwarn "home directory version control deactivated"
else  
  cerror "home directory is not under version control"
fi  

So when I'm not actively editing my dotfiles, I just run home and my home directory isn't a git repository until I run the script again.

Tags: linux, git