Syncing With Github

Have you ever tried to backup your folder to cloud and didn’t like the way it backups all the time? This occurred to me when I was trying to sync my obsidian vault using google drive. After a lot of research, I found that syncing with github to be the best option for me. And here’s how I did it.

Setting up the Repository

This is something you might have done many times if you’ve worked on open source projects. Here are the steps to creating a github repository.

Creating a Github Repository

  1. Sign in to github and click on the ’+’ icon on the top right corner.
  2. Click on New Repository
  3. Give a name to the repository, make it private and click on Create Repository.
  4. Copy the url of the repository

Pusing the Local Folder to Github

  1. Fire up a terminal and navigate to the folder you want to sync with github
  2. Enter the following commands in the terminal

    git init
    git remote add origin <url> 
    git add --all
    git commit -m "Initial Commit"
    git push --set-upstream origin <branch>

    Here <url> is the url of the github repository that you’ve already copied and <branch> is the name of the branch you’re working on, which in general is main or master.

  3. Next time you make a change to your folder, run the following commands:

    git add --all
    git commit -m "some_commit_message"
    git pull
    git push

Sync with Github

Doing this every time you make a change to your folder is very uncomfortable. So the idea is to automate the task. ie, to push the code to github at regular intervals if there is any change in your local folder.

Creating a Script to Commit the Changes


gstatus=`git status --porcelain`

if [ ${#gstatus} -ne 0]
    git add --all
    git commit -m "$gstatus"
    git pull
    git push

Write this code to a file called in your local folder. Give it executable permission using chmod u+x Now run the script using ./

Automate Syncing

We can automate the syncing process using a linux tool called crontab. This is built into linux system. Crontab uses the cron daemon to schedule tasks. These are the steps to schedule a job.

  1. Create a text file sync-cron.txt.
  2. Add the following text to the file

    */20 * * * * ~/<absolute_path_to_sync_file>/
  3. Schedule a cron job using the command crontab sync-cron

Here’s a video from Corey-Schafer explaining how to schedule tasks with crontab.

Bug Hunting in Scheduling

Now many times people get frustrated that their crontab is not running the scripts. This has happened to me the first time I did it. Here’s how I fixed it.

  • The most common issues are with the PATH variable being different from script and cron. To fix this I added the following to crontab

    */20 * * * * ~/<absolute_path_to_sync_file>/
  • Now in the script I added the PATH like this

    export DISPLAY=:0.0
  • You can also use an absolute path to your local folder with git cli.

    git -C <absolute_path_to_local_folder> add --all

A Word About Security

Is github safe? This requires another question to be asked. Is google drive, onedrive or dropbox safe? There is no answer to that. It is only them who know if it is safe. There is always a risk in storing personal data in cloud services. It would be a wise to encrypt your data before backing it to github if you’re storing personal information. But in general, github is a safe place even without encryption.


Github syncing is a flexible way to backup your data compared to other cloud services. You should try to backup relatively small data and be careful about storing personal information. Even though github is a code revision system rather than a backup service, it serves to be a better alternative compared to the others.

Profile picture

Written by Roshan R who lives in India, building useful things. You should follow them on Twitter