DEV Community

Cover image for Version Controlling with Git
Thushara Thiwanka
Thushara Thiwanka

Posted on • Updated on

Version Controlling with Git

In simple terms, Git is like a history book of your code and is used for tracking changes of the code from start to finish. Here, I will discuss the basics of git and a bit of GitHub along with examples.

What are Git and Github?

First, We have to know Git and Github are not referring to the same thing. Git is the most popular version controlling system in the world. The version controlling system records the changes that we do to our code throughout the development process in the kind of database called a repository. Using the repository we can find out who has been done changes to the code and when and why. Also, revert to any stage that has been saved in the repository. Without version control, we have to do this process manually by creating various folders and saving the multiple versions of the code. That manual process can take a much longer time and If multiple people are working on the same project this will become a tedious task. Also with Git, multiple people can work together, and each member will have their own snapshot of the code since git is a distributed version control system. So that they can work separately. Git became so popular because it’s free, open-source, fast, and scalable.

On the other hand, Github is an online service that hosts our version-controlled codebase. It makes sharing and working together easy with other developers. Those developers can download the code and make changes to the code and then re-upload the code to the online repository in Github and If it is according to our requirements it can be merged with the main codebase. Gitlab, Bitbucket, Sourceforge are the alternatives for Github. The below diagram shows the nature of git version control.

s

Getting started with Git

Here, I will be using the windows terminal throughout this process also we can use options in the code editor to do the same that will depend on the editor. The first thing we need to do is initialize git in the project that we are working on and start tracking files.

Navigate to the project that you need to be version controlled and use the “git init” command to initialize an empty git repository inside that folder named .git. Also, this repository will be hidden.

1

Then add the files that you need to be tracked using the “git add” command. This will add files to the staging area that needs to be committed. If you need to add one file to the stage area then you can use “git add filename” or If you need to add all files at once use “git add .”.

2

3

After staging the files run “git commit -m “commit message” ” to commit the changes to git and use the meaningful message to recognize the changes. These commits will be milestones for our project. So then, If your code will change frequently it’s better to add small commits from time to time.

4

Now your files are version controlled with git. However, you can use .gitignore file to add paths to files that you don't need to be tracked. Git will automatically look at this gitignore file and filter out any files that matches.

Top comments (0)