Linux Kamarada

Manage your dotfiles with Git

Dot what ???

What is commonly referred to as dotfiles are all those small plain text files that contain your softwares’ configuration. Most of the time they reside in your $HOME directory but are hidden as they are prefixed with a dot, hence their name. For some apps, you can find them as well in the $HOME/.config directory.

When it comes to managing them (i.e keep track of the changes, moving them around between different workstations, backing them up,…), there are different solutions:

  • the good old USB stick
  • rsyncing them
  • syncing them in the “cloud”
  • copying them in a central folder and symlinking them to where they are supposed to be found

In this article, we will focus on how to manage them in an efficient and simple way with Git.

Git comes to rescue

In this example, we will use $HOME/Dotfiles as the Git repository, but feel free to change it to your needs.

First of all, we will initialize this repository

1
git init --bare $HOME/Dotfiles

Then, as all the git commands that we will use will refer to this repository, it is advised to create an alias, such as:

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

You can add this line to your $SHELL configuration file ($HOME/.bashrc if you use Bash or $HOME/.zshrc if you use zsh).

Next, we will configure Git so it will not show all the untracked files. This is required as we use the entire $HOME as work tree.

1
dotfiles config --local status.showUntrackedFiles no

At that point, you should be able to check the state of this repository:

1
dotfiles status

Then you can add your configuration files and commit as you wish. For example, let’s add our .bashrc :

1
2
dotfiles add .bashrc
dotfiles commit -m "Added .bashrc"

Now just add a remote repository (your self-hosted Git or a public one) and push your changes to it:

1
2
dotfiles remote add origin git@yourgit.example.com/dotfiles.git
dotfiles push

Setup a new machine

Now that you have it all set, let’s configure a new system with the dotfiles you have in your repository.

First, clone locally your online repository:

1
git clone --bare git@yourgit.example.com/dotfiles.git $HOME/Dotfiles

Again, you have to defined the same alias as before:

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

Remember to put it in your $SHELL configuration file. Now, just apply the changes from the repository you have just cloned to your system:

1
dotfiles checkout

If some of the files already exist, you will get an error. This will probably happen with files created by default during the openSUSE installation and user account creation, such as the $HOME/.bashrc file, no worries, just rename or delete them.

Now, each time you change your configuration files tracked by Git, remember to commit and push your changes.

Sources

The following articles where used as sources of this article. Thanks a lot to their authors:

Would you buy me a coffee?
If you really liked it, if it was really helpful for you, is it worth a coffee? If you want, you can "buy me a coffee" with PayPal or contribute to the project in other ways.

Comments

About

The Linux Kamarada Project aims to spread and promote Linux as a robust, secure, versatile and easy to use operating system, suitable for everyday use be at home, at work or on the server. The project focuses mainly on distribution and documentation.

Ads

Facebook

Author