April 29, 2020

Dotfiles With Git

Simple way to manage dotfiles with git

I use a very simple way to manage my dotfiles with git and some bash aliases. All you have to create a folder for your git repository in your home folder and initiate a bare git repository

mkdir "$HOME/.dotf
git init --bare $HOME/.dotf

Then create an alias so you don’t have to write a long command each time you want to add a file or configure your git repository, don’t forget to add that alias to $HOME/.alias

alias dotf='/usr/bin/git --git-dir=$HOME/.dotf --work-tree=$HOME'

so, that alias is calling git and sets the repository as well as working directory and it can be called from what ever path you currently be in.

The next thing we need to do is to configure the repository to not show untracked files and only care about the files we specifically want to manage.

dotf config --local status.showUntrackedFiles no

Now you just use the dotfiles alias for git to add files, commit changes, etc, etc

dotf add .alias .bashrc
dotf commit -m "Added .alias and .bashrc"

To show the status of the repository

dotf status

if you want to add some bash completion to dotf you can just add the following line to your .bashrc

[[ "$(type -t dotf)" == "alias" ]] && [[ "$(type -t __git_complete)" == "function" ]] && __git_complete dotf _git

If you want you can add a remote repository make sure that it’s secure if you manage sensitive information.

dotf remote add origin <username>@git.domain.tld/dotfiles.git
dotf push