# Managing dotfiles

3 min read
Table of Contents

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 that we will use an alias to manage our dotfiles.

Terminal
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.

~/.zshrc
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.

Terminal
dotfiles config --local status.showUntrackedFiles no

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

Terminal
dotfiles add .zshrc

And commit them.

Terminal
dotfiles commit -m "Add zshrc"

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

Terminal
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.

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

And push our dotfiles to the remote.

Terminal
dotfiles push -u origin

My own dotfiles are on GitHub as well:

My dotfiles
00CSS

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.

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

Then we clone our dotfiles repository.

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

To check out our dotfiles we can use our alias.

Terminal
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.

Terminal
dotfiles submodule init --recursive
dotfiles submodule update --recursive
My avatar

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.


More Posts

Comments