Introduction to git and GitHub
Tags: git, github, tutorial, beginners
Published: 2020-07-22 12:28:31

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](https://dev-to-uploads.s3.amazonaws.com/i/9v62g6qgm0rs9mc8dnyc.jpg)

------------------------------------------------------------
# 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? &#129300;
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.

<center>
You write the code.  :computer:

You run it for the first time.  :running:

It works Perfectly.(In the first try..Ohhhhhhh) :flushed:

It is working as you intended. :cupid:

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

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

Fine!  :expressionless:

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. :disappointed: 

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.  :sunglasses:
BUT this is their reaction.
</center>
![Double the features](https://dev-to-uploads.s3.amazonaws.com/i/5s5bx0dua3xreubi5k2n.png)

<center>
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 :expressionless::computer:
You add the features asked. :computer:
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:
</center>
![Listen Here you little sh*t](https://dev-to-uploads.s3.amazonaws.com/i/13u6pehrqfj513gqyehy.jpg)

(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._*

<center>Now, it’s time for the technical terms</center>

-------------------------------------------------------------
#<center> What is git?&#129300;</center>
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.)_

<center>**_Its major disadvantage is that we have a single point of failure_**</center>

Linus Torvalds didn't like Centralised Version Control System, so he created Git
![Linus Torvalds Meme](https://dev-to-uploads.s3.amazonaws.com/i/l1jt1sm3zkr12uo511xv.JPG)


##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

-----------------------------------------------------
#<center> How does git work?&#129300;</center>

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

---------------------------------------------------------
#<center> Git Workflow&#129488;</center>

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](https://dev-to-uploads.s3.amazonaws.com/i/7tjoz7s41273wx9qperx.png)


##<center>To Understand this better, try and imagine shopping at a store  :convenience_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. 
</center>
-------------------------------------------------------------


#<center>What is GitHub?&#129300;</center>
- 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.

-----------------------------------------------------------

#<center>What is the difference between Git and GitHub?&#129300;</center>
- 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.

-----------------------------------------------------------

###<center> :confetti_ball: :tada: Congratulations :tada:  :confetti_ball: </center>
<center>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](https://ko-fi.com/nirbhayvashisht)**

**Follow me on [Twitter](https://twitter.com/nirbhayvashisht)** 

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.)

<center>Any feedback is appreciated</center>
[References](https://git-scm.com/book/en/v2)</center>