EDERSON DUARTE ITABAIANA

Dotfiles: The Best Way to Organize Your Dotfiles

Whenever you get a new machine, format your current one, create a new user, or even if you have multiple machines, it’s a repetitive, risky, and tedious process to restore all your dotfile configurations again.

What if I told you there’s an easy and secure way to do this? The technique is called a “bare git repository.” It involves storing your repository in a dedicated configuration directory, unlike traditional git repositories where the directory is named “.git”.

Don’t worry, I’ll walk you through the necessary steps to achieve this.

If this is your first time, follow these steps:

  1. Create the directory where the repository configurations will be stored:

    git init --bare $HOME/.cfg
    
  2. Create an alias to replace the traditional “git” command with “config” (this will become clearer later):

    alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
    
  3. Configure to hide unnecessary files:

    config config --local status.showUntrackedFiles no
    
  4. Add this new “config” alias to your path:

    echo "alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'" >> $HOME/.bashrc
    

Once everything is set up, the environment will be ready to use. Check out some examples below:

config status
config add .config/doom/init.el
config commit -m "Add doom emacs init"
config add .vimrc
config commit -m "Add vimrc"
config add .bashrc
config commit -m "Add bashrc"
config push

Cool, right?

Now, think about one of the scenarios I mentioned at the beginning of this post, where you need to restore a previous configuration.

Follow these steps to restore your configurations in a new environment:

  1. Create an alias to replace the “git” command with “config”:

    alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
    
  2. Ignore the directory where you will clone the repository to avoid recursion issues:

    echo ".cfg" >> .gitignore
    
  3. Clone your original repository as a “bare” repository:

    git clone --bare <git-repo-url> $HOME/.cfg
    
  4. Properly configure the alias:

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

After restoring, follow these steps to ensure the configuration is correct:

Done! Your configuration has been restored. If you’d like, run some commands to practice:

config status
config add .config/doom/init.el
config commit -m "Add doom emacs init"
config add .vimrc
config commit -m "Add vimrc"
config add .bashrc
config commit -m "Add bashrc"
config push

Conclusion

I’ve been using this technique for a few years, and I can say that managing dotfiles has become much easier, allowing me to focus on what really matters. I hope this technique has been as useful to you as it has been for me.