DEV Community

Ray Smets
Ray Smets

Posted on

How-To: portable, maintainable git and shell configs made easy

In case you have ever had to deal with getting your shell and git config dialed in on a new unix machine, this how-to is for you. It also for those of who you want to keep configs in sync across multiple machines.

I recently had to setup a brand new computer without being able to restore from backup and this little solution not only made the setup very simple and quick but also maintainable. It will be useful moving forward to keep a single source of config truth that is easily obtainable between machines.

In order to streamline a repeatable setup exactly as desired the (not so) secret is leveraging git and (fairly secret) symlinks.

Git clearly is great for the version control properties of the relevant config files (~/.gitconfig, ~/.zshrc, ~/.oh-my-zsh/custom, etc). Definitely keep these files (sans sensitive information) in version control.

However, how to manage config files that span multiple directories? The files listed above all live in the home directory, but what if a config file you want to track was in ~/dev/configs/myawesomeconfg? This where one of the advantages of symlinks come into play.

By creating a symlink from your config-repo directory to a config's regular path all disparate configs can be managed in one git directory. Furthermore by using symlinks, instead of hardlinks, one can then pull updates from the repo (say from another machine you work on) and the files that are symlinked in their default locations are also updated.

For example, I create a symlink for my .gitconfig from my config-repo, ~/dev/config-repo/.gitconfig, to its default location, the home directory. This is done by:

cd ~
ln -s ~/dev/config/.gitconfig .
Enter fullscreen mode Exit fullscreen mode

Now if that git managed .gitconfig file is ever updated and I pull the changes in config-repo locally the git config, in its "normal" place in the home directory is also updated. And visa versa if I update ~/.gitconfig the changes are reflected in git-repo change log. I use this same setup for a .zshrc config file.

This setup also works for my ~/.oh-my-zsh/custom directory. I allow each machine's oh-my-zsh installation to manage it's own installation, but I symlink the custom directory so I get all my shell aliases and functions.

cd ~/.oh-my-zsh
rm -rf custom
ln -s ~/dev/config/oh-my-zsh/custom .
Enter fullscreen mode Exit fullscreen mode

In conclusion, this solution enables tracking essential configurations in version control for maintainability and enables interoperability between machines. This can of course be extended to any type of config files / directories. I hope this how-to was informative and helpful.

Ray

Top comments (1)

Collapse
 
richwolski profile image
rich wolski

Hi Ray. I'm trying to reach you RE some UCSB business. Could you please contact me via email? Thanks. Rich