Disclaimer: This post is not about how to create dotfiles and what should be included in those dotfiles. I am expecting readers of this post to have their own basic/well-defined dotfiles. This post will be mainly focusing on how to manage your existing dotfiles like a pro!
Let’s start with a scenario where I have faced endless problems when I installed a new os on my desktop or got a new system from the office.
So, one day I got excited by reading one of a blog post on how to create your own dotfiles collection. Immediately after reading the post, I started collecting and adding different functionality to my dotfiles. This phase of creation was a bit time-consuming as I was searching different people's dotfiles and getting inspired from their cool dotfiles. After hours of inspiration and my own creation, I managed to create my own dotfiles. With excitement, I copied all my relevant dotfile into a directory and pushed it to a git repo. And one day I desktop crashed and I had to do a fresh installation of Linux os. After installation since I have a repo created for my dotfiles, I cloned the repo and then manually copied them to relevant locations. All good till now but the problem arises when you add new things to your dotfile, you add/change things to your dotfile and to make it available in the git repo you copy the files manually to the git directory and then push the changes. Phew! lots of manual work and not this not even close to syncing your dotfile with git.
One day I was browsing youtube and then I found a wonderful video on how to sync manage and sync dotfiles with git + gnu stow by DevInsideYou (if you like scala or want to start with scala then do follow this guy he is too good with his explanation and understanding of software development).
So, What is GNU Stow?
GNU Stow is well famous symlink farm manager. It manages symlinks from A to B. It takes distinct packages of software and/or data located in separate directories on the filesystem and makes them appear to be installed in the same place. For example,
/usr/local/bin could contain symlinks to files within
/usr/local/stow/perl/bin etc., and likewise recursively for any other subdirectories such as
.../man, and so on (example copied from GNU stow website).
In simple words once manage all the important files at different places in the os drive from one single directory by creating symlinks from the same directory.
How GNU Stow is helpful here?
So, one of the problems I mentioned earlier is after adding/changing my dotfiles I have to copy and paste into the git directory. With Stow now, I can create symlinks to different dotfiles from a single directory and whatever changes I am doing to dotfiles from the same directory will be reflected back to my CLI applications. Screenshots attached below — this shows how my different dotfiles symlinked to different files from the same git directory.
In the above screenshot, my 4 different dotfiles are now linked with my main dotfiles but from one directory. So whatever changes I will be doing to
Documents/dotfiles/stow/bash/.aliases , changes will be reflected back to
.aliases (super cool!) — no copy-pasting back and forth now.
Now I can take my
Dcouments/dotfiles directory and init with git then push it Github. When you install a new os and want to use your git synced dotfile again then just clone the repo and then Stow the dotfiles (pretty simple!).
How to Stow dotfiles?
This is will be a bit tricky but we gonna understand the whole process together!
Before Stowing dotfiles we first need to understand what should be the directory structure. We will take an example from the above screenshot
.zshrc . So, if we want to create a symlink for
.zshrcwe first need to navigate to the base directory and in this case it is
Documents/dotfiles. Since I have been managing other installation scripts in my git repo hence I have created a
stow directory. So, here in my case, the base directory will be
Documents/dotfiles/stow . Now I will create
zsh a directory and will put
.zshrc file in the directory.
Stow provides a handy way of checking if it gonna create the right symlink or not before creating the symlink. To check this first navigate to
Documents/dotfile/stow and then run the below command.
stow -nvt ~ zsh # ~ in this commands set ot use current dir as base dir
The above screenshot is the output for the above shell command. This confirms that Stow gonna create the right symlink. Now we are confirmed so let’s stow
stow -vSt ~ zsh
That’s it! we have successfully created a symlink for
Note: we can use the same stow command to stow
.aliases, .gitconfig, .zprofile
How to sync your dotfiles with Git?
Since our dotfile is in
Documents/dotfiles , we can git init the directory and push the newly created
.zshrc dotfile to Github.
My dotfile git repo: https://github.com/mlgruby/dotfile
Hope you find it useful, if yes please don’t forget to clap below 😊