Have you ever lost hundreds of solutions of competitive programming problems due to disk failure? Well, I have. Thus I have learned the importance of backing up stuffs the hard way. You can use various cloud storage platforms to back up for free e.g. Google drive, Dropbox, OneDrive etc. They are great for different types of files like images, documents, spreadsheets, PDFs etc. But when it comes to codes, my personal favourite is github. So here I am going to show you how you can use git and github to back up your codes seamlessly.

Disclaimer: This is by no means a comprehensive guide to git and github. The tutorial is simply to show beginner programmers and problems solvers how they can use git and github only to back up their codes. Git and github are a lot more than just backup tools. But that's not the focus of this tutorial. So if you are already a pro in version control systems, this guide is not for you!

What is git and github?

Git is a version control system. What it does is keep track of changes in files so that later you can specifically recall which version you want to check out. For example, you might have a bug free (!) working software. Now you want to add a new feature. So you start coding the new feature and at some point, you find out that you have broken the software by changing something that was stable before. You start to panic as you don't know how to get back to the previous stable version. How git helps here is that when you have a working stable version, you can make a commit i.e. create kind of a restore point and then start working on the new feature. If you break the software you can come back to that restore point any time you want. This is all you need to know about git  at this point.

Github is an online project hosting platform using git. You can use github to back up your local projects in an online repository. Github is primarily used for collaborative projects. Many open source community projects are hosted on github and programmers from all around the world contribute in them using git. However, you can also use github just to back up your codes in your own repository!

Why git and github?

Now you might ask, why git and github when you can simply drag and drop the files in a google drive folder! Getting to know and using new tools might seem intimidating at first especially when you start to see commands! (Yes we will be using commands in the terminal/command prompt.) So we must get some value out of it otherwise why bother making things complicated, right?

First of all, we are programmers. We should use tools built for programmers. I understand it's not a good enough reason why you should use git. But here's the thing, as I have mentioned before, git is not just a back up tool. It's a version control system. When you start working in large projects, especially when you work in professional environment and collaborative projects, you must know how to use a version control system. Hundreds/thousands of programmers working on the same project making changes here and there - can you imagine the scenario? How do you manage all that? Does google drive seem enough? No. You need more powerful tools and git and github together makes such a package.

Since you will have to use it at some point, why not take the opportunity to get used to it from now on? Start using git for the basic purpose of backing up. As you keep on exploring, you will figure out the beauty of it. If you are a student at this point, then later you can use it for group projects. You won't need to transfer code using pen-drive or facebook messenger or emails! So start using these tools.

The real stuff

Let's dive into how you can use git and github to back up your codes. There are lots of resources available online on git and github. They are really well written. You can also find video tutorials. Here I will help you establish a workflow to back up your codes and point to those resources for detailed procedures.

Install git

First you need to install git. Here you will find how you can do so on any platform.

Create repository

As you have installed git, now you need to turn your project directory into a git repository. The project directory can be a directory where you are coding your software. It may also be the directory where all you competitive programming solutions are saved. Since I am going to demonstrate the back up functionality of git and github, I will use competitive programming solutions for the tutorial.

Let the directory that holds all the competitive programming solutions be "competitive-programming". Open terminal/cmd and go to that directory using the "cd" command. Now initialize this directory as a git repository by simply running the following command.

git init

This command will create a directory called ".git". This will hold all the information related to this repository that you just initialized. If you no longer want to keep the directory as a git repository, simply delete the ".git" directory. Yes, it is as simple as that.

Add files

Your "competitive-programming" directory is now a git repository. Now you need to add files to the git staging area. It's nothing complicated. This step simply tells git which files to keep track when you create the next restore point. In order to add all the files in the repository, run the following command (the dot in the end is in the command).

git add .

If you want to select which files you want to add, then run the command like this.

git add file1.cpp file2.txt file3.py

If you want to ignore a directory or a whole class of files like all the executable files, then you need to create a text file called ".gitignore" in the repository. You can find details here but for now you don't need to think about it too much.


After adding the files to the staging are, you have to make a commit. This is where the restore point gets created.  A commit has to have a message with it which indicates what this commit signifies i.e. what changes have been made. To back up competitive programming solutions, you don't need to care much about this message. Just type in something! Here's the command.

git commit -m "your commit message in quotation marks"

At this point, you have locally tracked your files. You can see all the past commits using the following command.

git log

This shows too much details. A concise log can be seen by the following command.

git log --oneline

Actually backing it up!

Now you need to back up in github. First create a github account. Then create an empty github repository. For our case, we will name it "competitive-programming". In the newly created empty repository, you can see a section named "…or push an existing repository from the command line". This is what you will need to do and let me explain what's going on here.

Add remote

The first command is to add remote. Remote means a repository that is located in a remote server i.e. not in your local disk. You have created a local "competitive-programming" repository. What you need to do is add a remote repository for that local repository. Here, your newly created github repository is going to be the remote repository. By convention we name the remote repository "origin". So in you terminal, go to the local repository and run the following command as you can see in the github page.

git remote add origin link-to-the-new-empty-github-repository

Push code

You have added the remote repository. Now you need to push the local files to the remote repository. Run the following command.

git push -u origin master

This command pushes your local repository to your remote repository named origin. The master at the end of the command indicates the master branch. You don't have to care about it at this point because you won't need any other branch except the master branch. Branches are used in large scale projects to develop different features. You might need to enter your github username and password as you run this command.


This is it! Refresh the github repository page in your browser and you will see the files backed up there. This is not that complicated. Once you have initialized the repository and backed up the first time, you won't need to do all these any more. From there on, it will be just a few commands to back up your code.

For example, let's assume you have done this whole thing. Now you have your codes backed up and the whole repository thing set up. Now you have solved some new problems. To back them up, simply open up the terminal in the "competitive-programming" directory and run the following commands.

git add .
git commit -m "backup"
git push -u origin master

There you go, your latest solutions are also backed up! As you do this over and over again, it will become intuitive and you won't even need to think about it. This is what you should do instead of using google drive or any other drive. Getting used to version control system such as git will save you from a lot of trouble and help you in the long run. So explore git and github and start using them. Do collaborative projects using them. This will take you one step ahead.

Feel free to contact me if you need any kind of help!