Use Git To Manage Your Dotfiles

created: 2020-02-06

sneaked from: https://medium.com/toutsbrasil/how-to-manage-your-dotfiles-with-git-f7aeed8adf8b

If you are a Linux user like me and have done a lot of configuration in a few years of use, you should store your dotfiles in a safe place. Or even better, share them with others. In this little tutorial I’ll show you how to use Git to manage your dotfiles and upload them to github.com without any additional tools.

Create a .dotfiles folder in your home directory:

git init --bare $HOME/.dotfiles

Now create an alias for your dotfiles git command and store it in your .bashrc or .zshrc:

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

Set git status to hide untracked files: This is very important. Otherwise all files in your $HOME path will be noted as untracked.

dotfiles config --local status.showUntrackedFiles no

Usage: It’s done, now you can use all your known git commands. E.g.:

dotfiles status
dotfiles add .vimrc
dotfiles add .config/bspwm/config
dotfiles commit -m "initial"
dotfiles add .bashrc
dotfiles commit -m "Add: bashrc"
dotfiles push

If you are not sure which dotfiles you have edited, just run…

dotfiles status

… and you will see a list of changed tracked files.

Create a new github or gitlab repository and add it as remote for your .dotfiles repository:

dotfiles remote add origin https://github.com/USERNAME/REPOSITORY_NAME.git 
dotfiles push origin master

Checkout your .dotfiles repository on a new computer: Clone the repository as .dotfiles to your $HOME path:

git clone --bare https://github.com/USERNAME/REPOSITORY_NAME.git $HOME/.dotfiles

Define the alias showed before in your current terminal session (don’t safe it in your .bashrc oder .zshrc) and checkout the actual content from the remote git repository:

dotfiles checkout

Make sure that none of the tracked files and/or folders exist that you want to checkout, otherwise you will get error messages.