Willem Medendorp's Blog

Willem Medendorp's Blog

Managing dotfiles

📅
🏷️ [Dotfiles,Linux,Configuration]

Dotfiles are the configuration files inside your home directory that control how your system looks and behaves. They are called dotfiles because their name starts with a dot. This makes them hidden by default in most file managers. Dotfiles are used to configure many things on your system, like your shell, your text editor, and your window manager. In this tutorial, I will show you how I manage my dotfiles using Git. This way I can easily share my configuration between different systems and keep track of changes.

Setting up a dotfiles repository

We are going to create a bare Git repository in our home directory to track our dotfiles. After which we will use a alias to manage our dotfiles.

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

Then we will create our alias in our shell configuration file. This way we can easily manage our dotfiles.

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

Now we can use dotfiles as if it were git to manage files. The first thing we want to do is to ignore all files in our home directory except the ones we want to track using our new alias.

dotfiles config --local status.showUntrackedFiles no

now we can start to add files we want to track.

dotfiles add .zshrc

and commit them

dotfiles commit -m "Add zshrc"

To have our alias available in every new shell we can add it to our shell configuration file.

echo "alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'" >> $HOME/.zshrc

What are important dotfiles?

We could track all the files in our home directory, but that would be overkill. Instead, we focus on the most important ones which we can use to recreate our setup on a new system. The dotfiles that I include are related to my shell, my text editor, and my window manager. Dotfiles for random applications that I use are not included.

Sharing and backing up your dotfiles

Now that we have our dotfiles in a Git repository, we can easily share them with others. We can push our dotfiles to a Git hosting service like GitHub or GitLab. This way we can easily clone our dotfiles on a new system and have our setup ready in no time.

To do this we first need to create a new repository on GitHub or GitLab. Then we can add it as a remote to our dotfiles repository.

dotfiles remote add origin git@github.com:WillemMe/dotfiles.git

and push our dotfiles to the remote.

dotfiles push -u origin

Configuring a new system

To configure a new system with our dotfiles we first need to set our alias in the shell configuration file. After which we can clone our dotfiles repository.

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

Then we clone our dotfiles repository.

git clone --bare --no-checkout https://github.com/WillemMe/dotfiles.git .dotfiles

To prevent conflicts with existing files you might want to backup your existing dotfiles.

To checkout our dotfiles we can use our alias.

dotfiles reset --hard HEAD

Now we have our dotfiles on our new system and we can start using them. If our dotfiles contain nested Git repositories we can use the following command to clone them.

dotfiles submodule init --recursive
dotfiles submodule update --recursive

Copyright 2024
Willem Medendorp

made with
and