Introduction to git and GitHub

nirbhayvashisht

Nirbhay Vashisht

Posted on July 22, 2020

Introduction to git and GitHub

Before we start, I want to state that this tutorial is for absolute beginners who want to learn git and GitHub.

The tutorial has no specific prerequisites

After reading this tutorial you’ll be able to answer the following questions:

  • What is git?
  • How does git work?
  • What is GitHub?
  • Why do we use them?
  • What is the difference between git and GitHub?

Let's do this one last time


Introduction

In layman’s terms, git is used to save different versions of your project.

Now the question arises,

Why backing up different versions of your project is important? 🤔

To understand this, let’s move to an alternative universe with no version control system.

In that world you are working for your dream company. Your team has been asked to work on an important project and your job is to build the back-end.


You write the code. 💻

You run it for the first time. 🏃

It works Perfectly.(In the first try..Ohhhhhhh) 😳

It is working as you intended. 💘

It’s the best code that you have ever written in your life. 😍

You pass it to your team members and..........
somehow.....
they frick that up. 😱

Fine! 😑

It comes back to you and you have to fix it because you did not have any backup.

Now you fix it. But it’s not that perfect as it used to be. 😞

Then your teammates do their part. Your team leader shows it to the project manager. The project manager shows it to the client.

They like it. 😎
BUT this is their reaction.



Double the features

So now they ask you to DOUBLE the features. They are paying you for that. It’s your job. You have to do it.

So, you work on the logic. AGAIN 😑💻
You add the features asked. 💻
It is passed on to your team members and they do their part.
Your team leader shows it to the project manager.
Your project manager shows it to the client.

And this is their reply.

“I think we like the previous one”

Now you cannot go and be like:



Listen Here you little sh*t

(You have to be polite and do as he says because he’s not a little crap. He’s a big crap and he can do a lot of crappy things to you and your career.)

I think you get the point that why we should back up different versions of our project.

Now, it’s time for the technical terms

What is git?🤔

Git is a Distributed Version Control System created by Linus Torvalds in 2005 for the development of the Linux Kernel.
(Note the terms "Distributed" and "Version Control Systems")

What is a Version Control System?

Version control systems are a category of software tools that help a software team manage changes to source code over time.

Version control software keeps track of every modification to the code in a special kind of database.

(In Layman’s term: It is like that save game mechanic for your software development)

Before Distributed Version Control System, Centralised Version Control System was popular.

Let's have a look at Centralised Version Control System.

  • In centralised version control system we have a server and a client.
  • The server is the main repository which contains all the versions of the source code.
  • Client fetches the code to local machine, works on it then commits it to the server.

(If you didn’t understand words like commits and repository. Hold your horses. We’ll look at them later)

Layman’s Language

In centralised version control system, we have a single server that contains all the versioned files and the clients check out files from a central place.

So basically, to work on any project, firstly the user or client need to get the code from the server or main repository.

Once you get the latest version of code, you start making your own changes in the code and after that you simply needs to commit those changes straight forward into the main repository.

(Here committing your changes means to merging your code to the main repository or making a new version of the source code.)

Its major disadvantage is that we have a single point of failure

Linus Torvalds didn't like Centralised Version Control System, so he created Git
Linus Torvalds Meme

What is a Distributed Version Control System?

Being distributed means that you do not have one central location that keeps track of your data.

No single place is more important than any other single place.

In distributed version control system, the client doesn’t just checkout the latest snapshots; rather they fully mirror the repository, including its full history


How does git work?🤔

Let us first look into some of the important terminologies:

  • Snapshots

    • It is the way in which git keeps track of your code history
    • A snapshot is essentially a recording of what your file looks like at a given point in time
    • You decide what you want to include in a snapshot and what you want to exclude
    • You have the ability to go back to any snapshot
  • Commit

    • The act of creating a snapshot.
    • You project is essentially bunch of commits.
  • Repository

    • Often called "Repo"
    • A collection of all the files and the history of those files

Workings of git

With Git, every time you commit, or save the state of your project, Git basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot.

If files have not changed, Git doesn’t store that file again, just a link to the previous identical file it has already stored.

The mechanism that Git uses for this check summing is called a SHA-1 hash.

A SHA-1 hash is a 40-character string composed of hexadecimal characters (0-9 and a-f) and calculated based on the contents based on the contents of a file or directory structure in Git.

A SHA-1 hash looks something like this :
24b9da6552252987aa493b52f8696cd6d3b00373


Git Workflow🧐

The basic Git workflow goes something like this:

  1. You modify files in your working tree.

  2. You selectively stage just those changes you want to be part of your next commit, which adds only those changes to the staging area.

  3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

Git has three main states that your files can reside in: modified, staged, and committed:

  • Modified mean that you have changed the file but have not committed it to your database yet.

  • Staged means that you have marked a modified file in its current version to go into your next commit snapshot.

  • Committed means that the data is safely stored in your local database.

Checkins over time image

To Understand this better, try and imagine shopping at a store 🏪

You pick the items.
You add them to the shopping cart.
You go to the counter and create the bill.

The bill is your commit.
The shopping cart is your staging area.
There are various items in the store, but the only things added to your bill are the items you put in the shopping cart.

What is GitHub?🤔

  • GitHub is a United States-based global company that provides hosting for software development and version control using Git.

  • It offers the distributed version control and source code management (SCM) functionality of Git, plus its own features.

  • It provides access control and several collaboration features such as bug tracking, feature requests, task management, and wikis for every project.

  • It is the largest host of source code in the world.


What is the difference between Git and GitHub?🤔

  • Git is a version control system while GitHub hosts your git repositories(ie. puts them on your GitHub account and makes them accessible from anywhere in the internet)
  • Git is a command line tool while GitHub is a website.
  • Git is installed and maintained on your local system (rather than in the cloud), GitHub is a cloud based repository hosting service.

🎊 🎉 Congratulations 🎉 🎊

You have completed this tutorial "Introduction to git and GitHub"

If you liked this tutorial consider supporting by Liking and Sharing this article.

You can also Buy Me a Coffee

Follow me on Twitter

If you want me to make a tutorial on any computer science concept then leave the topic down in the comments.
(I might make tutorials on git commands/GitHub workflow if people ask for it.)

Any feedback is appreciated



References
💖 💪 🙅 🚩
nirbhayvashisht
Nirbhay Vashisht

Posted on July 22, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related