Versioning and syncing Linux config files
We want to keep our config files organised and synced over multiple devices. We don't want to have our entire
home directory as a git repository, because that would interfere with git repositories
inside the home directory (e.g. in ~/git/).
We solve this by creating a git repository in some subdirectory of $HOME and
defining aliases that let us conveniently work on it with the work tree set to $HOME.
Prerequisites
- Have
gitinstalled. - Have an empty directory in your home directory. I used
$HOME/dotfilesfor this example. If you choose something else, you must edit the commands below accordingly. - If you want to sync across multiple devices: have an uninitialised
gitrepository at GitHub or GitLab (or some other service), preferably a private one.
Initialisation
- Navigate to your home directory.
- Run
git init --bare dotfiles - Add
alias config='/usr/bin/git --git-dir=$HOME/dotfiles/ --work-tree=$HOME'to your.bashrc - Restart your shell
- Run
config config --local status.showUntrackedFiles no - If you want syncing, run
config remote add origin git@github.com:someuser/somerepo.gitand thenconfig push -u origin master. Replace the URL in the first command with one pointing to your own remote repository.
Versioning and syncing
You can now use your home directory like an ordinarygit repository.
This works by using the alias config, which we defined in the previous step.
For example, we can version our .bashrc like this:
$ config add .bashrc$ config commit -m "Versioned .bashrc"$ config push