DEV Community

Cover image for How to backup/sync all of your dotfiles​ with Github
Jeff Shomali
Jeff Shomali

Posted on

How to backup/sync all of your dotfiles​ with Github

As a developer, one of the challenges that we are facing every day is to backup or sync up all of our dot(.) files with a new or another environment. There are many dotfiles in my machine such as .alias, .vimrc, .zshrc, .gitconfig etc...

I'm not going to explain how to get a back up of all your files because each one of us has a different environment, so I'll explain how to backup your VS-Code setting. I know there is a VS-Code extension that syncs up your setting with Github. Our goal is to understand how to back up #vscode settings, snippets, extensions, and sync it with Github manually.

Create a Script

  • Create a new Github repository in your
  • In your local machine go to the desktop and open an empty folder in your terminal
  • Initiate an empty Git repository with $ git init and connect the local repo to newly created repo with $ git remote add

  • Create a shell script and name it whatever you want, like and past the following code on that file.


# check to see is git command line installed in this machine
IS_GIT_AVAILABLE="$(git --version)"
if [[ $IS_GIT_AVAILABLE == *"version"* ]]; then
  echo "Git is Available"
  echo "Git is not installed"
  exit 1

# copy Vs-Code files
cp  $HOME/Library/Application\ 
Support/Code/User/{keybindings.json,settings.json,spellright.dict} .

# copy snippets folder
cp -r $HOME/Library/Application\ Support/Code/User/snippets .

# copy list of extensions that currently installed
code --list-extensions --show-versions >> ../../vscode/extensions.txt

# copy other dot files 
cp  $HOME/{.zshrc,.vimrc} .

# Check git status
gs="$(git status | grep -i "modified")"
# echo "${gs}"

# If there is a new change
if [[ $gs == *"modified"* ]]; then
  echo "push"

# push to Github
git add -u;
git commit -m "New backup `date +'%Y-%m-%d %H:%M:%S'`";
git push origin master
Enter fullscreen mode Exit fullscreen mode
  • Change the script permission with $ chmod +x
  • Run the script with $ ./

If you want to run in background services:

  • crontab -e and add your script path and time you want to run the script for example
$ crontab -e  # this is open the cronjobs table in vim mode use i for insert mode

# this is run the script every minutes
$ 1 * * * * cd /Users/macp15/Projects/dotfiles/scripts/backup && ./

# display list of cron jobs
$ crontab -l
Enter fullscreen mode Exit fullscreen mode

Done (Khalas).

Top comments (5)

bnikanjam profile image
Babak Nikanjam • Edited

Great explanation, very useful and real problem solver. Nice touch on having it run as a background service automatically (periodically). I love these kind of short and neat learning drops. Thanks Jeff.

msfjarvis profile image
Harsh Shandilya

VS Code has an extension that implements most of this minus the automatic sync (AFAICT) called Syncing that uses Gists instead of a full repo.

sientelo profile image
Paul Murray

Thank you, this is the easiest dotfiles backup solution I have found. Question - when restoring the dotfiles to a new machine, is it just a matter of basically pulling the remote Git repo to the new machine and then running ./ ? I did this but my .dotfiles in my new home directory are not updating with the files created in the /dotfiles directory

notaduck profile image

Nice article, but I think you should take a look at stow! It is amazing for managing your dotfiles.

bgopikrishna profile image
Gopi Krishna

Thanks, Great article.