How to store configuration files in git
I store my configuration files as a git repository. This gives me the following important features:
- I can easily setup configuration on a new machine, simply by cloning the repository.
- I can have different branches (currently I am using them for different operating systems).
- I can easily rollback any changes (even though I edit my configuration files in vim and I have persistent undo enabled).
Requirements
To implement what is described here, you obviously will need git. I suppose that you have some remote place to store the repository (I am using my own remote server and gitolite but I suppose that anything – for example, GitHub – will work as well). Also using a shell which allows to set up aliases makes things more convenient. I am using GNU Bash and PowerShell and both of them work.
Initial setup
To start, create a bare Git repository. I call mine .dotfiles
and store
it in my home directory.
mkdir ~/.dotfiles
cd ~/.dotfiles
git init --bare
Now create a remote repository (if you plan to have something non-private in
your configuration, like SSH or Amazon keys, make sure it is not publicly
accessible). Add the fresh remote to your repository with git remote add
.
Usage
Alias
My alias is named idf
(stands for Invoke-DotFiles
) but, of course, it can
be named as you wish. It calls git
using home directory as a work tree and
.dotfiles
as a git directory:
alias idf='git --git-dir ~/.dotfiles --work-tree ~'
Configuration
I strongly advise to configure git
not to complain about untracked files in
your home directory: idf config --local status.showUntrackedFiles no
.
Adding new configuration files and editing existing ones
Use the idf
alias in the same way you would use git
. To add a new
configuration file: idf add <path to file>
, then idf commit -m <commit message>
. To propagate the changes to the remote repository: idf push
. To
get the changes to the other machines, run idf pull
there.
The same procedure works for editing existing configuration files.
Usage on a new machine
Do a bare clone of the existing remote repository:
git clone --bare <remote repository> ~/.dotfiles
Set up an alias and ignoring of untracked files as described above. Now use
idf checkout
to your heart’s content (you might need to do idf fetch
once).