Versioning control with GIT and GITHUB

Before starting, you can also find the step by step video guide here, on my YouTube channel (/c/cppdev).

The local setup for this tutorial is Linux Ubuntu.

I find Linux to be a better fit for programming, than Windows. So I will cover the steps only for Linux in this case (but setting up GITHUB repository and project is common for both, only locally working is different).

Preface

Let’s say that you started your own project. You already have a big part of the source code but the project starts to be quite big and you can’t handle it alone anymore. Or even worse, since you have your source code on your PC, something maybe happens to it and you lose it all. That sucks, right ?

This is where the versioning control (or Version Control System) comes in.

The version control is a class of systems responsible for managing changes for the source code (or documents, large websites or other collections of information). Changes are usually identified by a number or letter code, termed the “revision number”, “revision level”, or simply “revision”. For example, an initial set of files is “revision 1”. When the first change is made, the resulting set is “revision 2”, and so on. Each revision is associated with a timestamp and the person making the change. Revisions can be compared, restored, and with some types of files, merged.

So, you will be able to keep all the ‘versions’ of your files in a repository. The repository will be your storage and the VCS will be your storage management tool.

Now, to get to the point, how to create your own repository in GITHUB and set your VCS ? I will show you in just a moment.

1.Setting up a new project and the repository in GITHUB

This step is quite easy to do, since all of this will be done from the GITHUB GUI in the browser.

After you log in to your GITHUB account, in the top rightmost corner, click on your profile -> "Your projects" button.

In the new window, click on the green "New project" button.

Now, we will have to set up some details for the project. In the given window, we will set the "Project board name" and the "Visibility". We will link the repository later on, after we create it.

You can set the visibility however you like here. If you set it Public, then everyone will have read access to your project.

After everything is set, just press the green "Create project" and we have the project all set.

We only have to create the repository and link it to our project, then we are all done here, on the GITHUB GUI for now.

In order to create the repository, we will follow quite similar steps as the ones for creating the project.

First you need to click on your profile -> "Your repositories" .

Then click on the green "New" button.

In this new screen, we have to set up some details, as did before for the project. We will set the "Repository name", "Visibility" and what I always like to do, is to check the "Initialize this repository with:" "Add a README file".

After everything is set, just press the green "Create repository" button and we are good to go.

Now we have both our project and our repository set up in GITHUB. We can use the repository to add our source code there.

2.Setting up GIT binary utility (in Linux Ubuntu)

Now that we created the project and the repository, we need the GIT utility in order to be able to “clone” or “commit” and “push” our changes to this repository.

I will cover how to install the GIT utility for Linux Ubuntu only, but you can find plenty of information on how to set up GIT for Windows on the internet.

In order to install the GIT utility for Ubuntu, you only need one simple command : "sudo apt-get install git".

That’s it. Now you have the GIT utility installed and you can “clone” your repository so you can start working on it. “Cloning” is just a fancy word for getting the changes locally, from your repository, so you can update them and add them to the repository afterwards.

3.Cloning your repository

In order to clone your repository, you also need a single command in Linux. You first need the link to the repository, which you can get from the GITHUB GUI.

First, go in the GITHUB GUI, click on your profile -> "Your repositories" then click on the repository name that you have just created. In my case, this is "thecppdev-repository" .

On the repository page, click on the green “Code” button, then copy the link that is shown there(for https, in this case). In my case, the link to my repository is : https://github.com/w0wy/thecppdev-repository.git

Now that we have the link, we go back to our machine and just use the command git clone <repository-link>. In my case, the full command will be git clone https://github.com/w0wy/thecppdev-repository.git

Note: in the picture, it’s the repository that I have used for my YouTube series (Project from scratch).

4.Project setup with directory structure

Now, whatever changes we do in the directory that we have “cloned”, we can “commit” and “push” to the repository and we will have access to it later on. The repository works as a storage space.

What I like to do with the directory structure is to create three directories in the main one : "src", "include" and "test".

Note: again, this is the repository used in the series, but the structure will be exactly the same.

Now, in each directory, we have to create at least one file, since the VCS is handling files, not folders. If we only have a new folder and we commit it and then push it, it will not be seen in the repository. We need to have changes on files.

We are going to create some dummy include and test files for include and test directories and a "main.cpp" file for the src directory.

With this, we have the project directory structure.

This is just how I structure my projects. If you already have a project done, you can just add your directories and files and commit and push them to the repository directly.

5.Commiting and pushing the changes

In order to commit changes to the repository, we need to first “stage” them for commit.

We are going to do this with the command "git add <files/directories>". In our case, the full command will be "git add src include test" . We can check which files are staged for the commit using the "git status" command.

Now we only need two more commands in order to have everything on the repository. First, it will be "git commit -m <commit_message>" (the message will appear in the GITHUB GUI). The full command in this case will be "git commit -m "adding project structure"". You can use whichever commit message you see fit. Usually these are going to be the description of the changes that you have done.

After the commit is done. The last thing that we have to do is to use "git push" command. Using this command might also require you to add your GITHUB user and password.

Now we have everything and all we have to do, is just check the GITHUB GUI in order to see if the changes are there.

6.Repository overview on GITHUB

When we want to check for the changes in the GITHUB GUI, we just have to go and click on our repository. The same instructions that we did when we tried to get the repository link in order to clone it.

If we check there, in the GITHUB GUI, we can see the changes and also the commit message that we added.

As you can see, we have the “adding project structure” commit message there.

You can also click on the "x commits" link and you will be able to see all the previous commits there together with the commit messages for each one.

This is it. You can now create your own project and your own repository and put it under version control. This will help you keep the code clean and also synchronize any changes you do, in case you are working as a team.

This is just a beginner’s guide and I did not go into many details related to GIT or VCS in general. I hope you learned something from this and whish you the best of luck with all the projects that you plan on doing.

Leave a Reply

%d bloggers like this: