Version Control - Basics

What is Version Control?

Version Control can be described as a system that helps us to manage the changes that are made to files over time, including code, binary or digital files. It helps by recording and keeping a track of the modifications done to a file or to a set of files so that at a later point of time we can recall a specific version of the files(s) if required.
Another benefit of using a version control system is that it makes collaboration among software engineers working on a particular project easy and also provides some insights on what everyone is doing on the project. In this article, we will understand version control in detail and  for that we will be talking about Git and GitHub.
Git is a distributed version-control system that tracks the changes of file(s), whereas GitHub is a cloud-based hosting service that is built around the Git tool.



Remote Repository vs Local Repository:

Suppose you are part of a team whose task is to build a chat app. Now your responsibility is to set up the working repository. So for that, you create a folder in your local system. After creating the folder, it has to be initialized as a git repository using the command  git init . After this, the project folder residing in your system becomes a local git repository. Then you add your initial project files and commit those changes locally. Now for the whole team to contribute, your local repository has to be available somewhere on the cloud. This is where GitHub comes into the picture. A repository, ideally with the same name as your local folder, needs to be created on GitHub. Now using the URL of this repository you can push the contents of your local repository to the one on GitHub. So this becomes the remote repository. Now other members can clone this repository on GitHub and all the initial project setup will be available to them.
        
Create a folder for the project in your system. Then initialize it using git init. It becomes the local repository
 
Now go to github.com and create a repository with the same name. This becomes the remote repository

Now using the url of the remote repository, connect your local repository to the remote one

Now after adding your code files, when you push your code it gets pushed all the way back to the remote repo

3 Areas of a Git Repository:

Inside a Git repository we have three logical areas in which we work with our files - The Working Tree, The Staging Area and The Git History.


  • The working tree is what we see in our local system, where we add, edit or delete files
  • When we make changes to our files, Git gives us the full freedom to choose which files to add/include in the next commit. After adding a file using the  git add  command it goes into the staging area
  • The Git History may be defined as a hierarchical structure, which contains a list of all the commits done on that repository over time.  

Git Branching:

Branches allow us to work on the same file in parallel. When we initialize a repository there is a basic branch by default which is the master branch. Now when we create a branch, a different copy of the whole working tree is created. Like suppose we create a branch named chat-room from the branch master, then in the newly created branch we will have the exact copy of all the files that were in master. We can add, edit or delete files in this branch independently. This will not affect the master branch. The branching feature is used by developers who are working on the same project but on different features. From the main branch, different feature branches can be created where different developers can work independently. When a branch is stable and tested then that branch can be merged with the main branch.
The command  git branch <branch-name>  is used to create a branch. Then we can go to that branch using  git checkout <branch-name> . For merging, generally a pull request is required to be raised in the remote repository, i.e on Github and a reviewer will review your changes and merge the branch with the main branch, or for a local merge we can just go back to the main branch and use  git merge <branch_name>

Create and checkout to a new branch. After adding /editing files commit and push the contents of this branch

When we push the code, in the remote repository the new branch gets created


To merge with the master, checkout to master and merge the branch using git merge command.

Git Logs:
All the commits made in a repository can be viewed using the git log command. Each of the commits has a unique hash value. Using that unique value we can restore back our files at that particular commit. We can move to a particular commit using the command  git checkout <commit_hash> .

The git log command gives list of all commits and the unique hash value, using which we can checkout to that commit.


Hope after reading this article you get a basic idea of what version controlling is and also some understanding of git. There are more advanced stuffs related to version controlling and Git which I will be posting in my upcoming articles. Do comment out your views on version controlling. Stay Safe and Happy Coding!

Post a Comment

0 Comments