Learn basics of Version Control & Git Commands in less than 10 minutes.

Romesh Liyanage (Romeliyan)
HackerNoon.com
6 min readJun 22, 2019

--

Introduction to Version Control & Git Commands

What is Version Controlling? 🤔

“Management of multiple revisions of the same unit of information.”

Version Controlling is managing changes to a source. These changes are identified by a revision number. Each revision has its timestamp as well as the person who is responsible for the change.

Revisions can be restored, compared and managed.

Why is Version Controlling used?

  • Easier backups and centralized source code repository.
  • Easy collaborative development.
  • Overview of changes performed to a file.
  • Access Control.
  • Resolving Conflicts.

Terminology

Repository — Central location where all files are being kept.

Trunk — Also referred to as the master branch. Most stale code ( production code ) is being placed here.

Stage — Mark files for tracking changes.

Commit — Create a snapshot of changes being made to files.

Branch — Copy of the master branch taken in a given point.

Checkout — Mark/Unlock file for changing.

Merge — Combine branches together to update the master branch.

Merge Conflict — Changes that interfere with other changes.

Best Practices

When version controlling, there are some best practices that should be followed in order to get the best results. Some main best practices in version controlling are as follows:

  • Use a source control system.
  • Always make sure to have the latest version of the file.
  • Check out only what you need.
  • Merge code with the development branch at least once per day.
  • Always make sure code is working as expected and not causing any other code to break.
  • Follow a formal review process when merging.

Version Control Systems

There are many version control systems. Some popular Version Control Systems are:

  • Git
  • Mercurial
  • CVS
  • SVN/Subversion
figure: 1.1-Git

Git is the most popular version control system.

It is a distributed version control system which allows a client to get a complete clone of the source code.

In a disaster situation, full source along with all history can be restored from a client.

It’s also free and open source.

It can contain multiple branches and tags. (Feature branches, role branches).

It is faster compared to other systems since it works on a Linux kernel and is written in C. Also it supports multiple protocols like HTTP and SSH.

Basic Git Commands

git init — initializes local Git Repository.

git clone — clones Repository into a new Directory.

git add — adds the particular file to index.

git add . — adds files to index.

git push — pushes to Remote Repository.

git pull — pulls latest from Remote Repository.

git status — checks the status of the working tree.

git commit -m your commit message— commits changes made to files.

Now that we know what Version Control is and what Git is and its basic terminologies, let’s see how we can place a file under git.

Make a GitHub Account.

If you don’t have a GitHub Account you can make a new GitHub account here.

Install Git on your machine.

If you are using a Mac…

You can enter the following command in the terminal.

If you already have Git installed on your machine, it will show which version of Git you have installed. If you don’t have Git installed yet it will install the Git in your machine.

If you are on Linux …

You can enter the following command in the terminal.

If you are on Windows…

You can download the latest version of Git from here and get installed it to your machine. It will install an applicaction called Git Bash which is an application for Microsoft Windows environments which provides an emulation layer for a Git command line experience.

figure: 2.1-Git Bash

Configure your Git username and email address.

As the next step you have to configure your username and email address. These details will be associated with any commits that you create. To configure your username and email enter the following commands on the terminal replacing romesh’s name and email address with your name and email address.(if you are on windows enter the commands on GitBash )

Create a Repository on GitHub.

This can be done by login in to your GitHub account and selecting the new button shown in the figure 3.1

figure: 3.1-New Button which used to create a Repository

This will navigate you to the repository creation page. Here you can specify your name, description, type of your repository and other requirements.

figure: 3.2-Create a new Repository

Locate the folder where you want to place the git.

Locating the folder can be done in various ways. You can use the “cd” command in the terminal as follows to locate the specific folder:

or if you are on Windows you can go to the specific folder and type “cmd” on the path tab and press enter.

figure: 3.3-Inside NewGitFolder

or you can go to the specific folder and right click and select “git bash here” option.

figure:3.4-Inside NewGitFolder

Initialize Git.

After locating the folder, you have to initialize Git using the following command in the prompted command line or git bash window.

Add files to the staging area.

Now you can create or change your project in this location. After the changes you have to add the particular files to the staging area for the commit. Use the following commands:

Check Status.

Before committing, we can check what files are staged by entering the following command in cmd or in git bash.

It will list all new or modified files to be committed. If it shows new or modified files, you should have to commit those changes which you added to your repository.

Commit Changes.

The message inside the “ ” is given so that the other users can see what changes you have made. Also if you want to Uncommit changes you just made, you can use the following command and unstage the files you just added.

Add a Remote Origin and Push.

Now each time you make changes in your files and save them, it won’t be automatically updated on GitHub. Therefore, to update the changes to the master:

Now the “git push” command pushes the changes in your local repository up to the remote repository you specified as the origin.

Voilà. You have just added the files to your new repository on GitHub. 🤗

But, how you download and work on repositories on GitHub? 🤔

Cloning a Git Repository.

In this instance also, first you have to locate the directory where you want to clone the repository. Then copy the link of the particular repository you want and enter the following command in cmd or in git bash.

Git Pull.

If two or more developers are collaborating on a project, all the users are working on the same project. Each time you make changes and push it into the master repository, others have to pull the changes that you pushed. This means you have to make sure that all are working on the same/latest version of the git repository.

And that’s it for the Introduction of Version Controlling and Git. 😎

Thanks for reading.❤️

If you have any queries regarding this article, feel free to comment in the comment section or contact me on Twitter.

--

--