Last Updated: September 29, 2021
·
16.01K
· itseranga

Keep your dotfiles in git

What are dot files

  • Dot files are hidden files. You have configured a lot of your own settings, configurations, or themes within dotfiles. Following are some example dot files
~/.vimrc 
~/.bashrc
~/.bash_profile
~/.zshrc

Purpose of keeping dot files in git

  • When you switching between machines(for example move to new machine) it can be difficult to keep your configuration files synchronized across them

  • One solution is to put your dot files under version control

  • By storing your dotfiles in a Git repository, you’ll be able to use them on any OS X or Linux machine with Internet access

  • In this example I'm gonna keep my .bash_profile and .zshrc in git repository in github.

How to do that??

  • Create folder call ~/.dotfiles in your home directory (its a hidden directory)
mkdir ~/.dotfiles
  • Move your ~/.bash_profile and ~/.zshrc files to ~/.dotfiles
mv ~/.bash_profile ~/.dotfiles/bash_profile
mv ~/.zshrc ~/.dotfiles/zshrc

Note that the files in ~/.dotfiles directory are not hidden(not with . prefix )

  • Create symlinks to moved files
ln -s ~/.dotfiles/zshrc ~/.zshrc
ln -s ~/.dotfiles/bash_profile ~/.bash_profile
  • Create github repository(In my scenario I have created repo name dotfiles). After creating the repository, github direct you how to push files to it

Picture

  • According to the instructions I have made my local ~/.dotfiles directory as a git repository and commit the changes
cd ~/.dotfiles
git init
git add bash_profile
git add zshrc
git commit -m "initial dot files"
  • Configure your git remote configurations in your local git repository and push the changes to the github
git remote add origin https://github.com/erangaeb/dotfiles.git
git push -u origin master
  • You are done :) .. you can add more configuration files to .dotfiles directory and keep them in github

This is my github repository - https://github.com/erangaeb/dotfiles

Install your dot files in another machine

  • Once your configuration files(dot files) is under version control, it’s quite straightforward to import your settings to any machine that has git installed

  • I have added the instruction in README file

Installation

    git clone https://github.com/erangaeb/dotfiles.git ~/.dotfiles


Create symlinks

    ln -s ~/.dotfiles/zshrc ~/.zshrc
    ln -s ~/.dotfiles/bash_profile ~/.bash_profile

3 Responses
Add your response

You can create a small install script for your dotfiles that will create symlinks and possibly do other stuff. Here's mine:

https://github.com/dpashkevich/dotfiles/blob/master/cli/install.sh

over 1 year ago ·

The other thing is to be careful what info you add, e.g. you don't want to share your ssh keys on github.

over 1 year ago ·

@dpashkevich Thanks :)

over 1 year ago ·