DEV Community

Nirbhay Vashisht
Nirbhay Vashisht

Posted on

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

Top comments (25)

Collapse
 
mikel_brierly profile image
mikel brierly

This was fantastic!! I have to admit that it took me waaaaaaay too long to grasp this concept:

"What is the difference between git and GitHub?"
"Git is a command line tool while GitHub is a website"

What an awesome article for beginners and experienced folks alike!

Collapse
 
nirbhayvashisht profile image
Nirbhay Vashisht

Thanks for the feedback Mikel.
Such comments encourages me to work hard on the content ^_^

Collapse
 
ashwinsharmap profile image
Ashwin Sharma P

Good article Nirbhay. πŸ‘
Explained the details so well using memes and analogies. πŸ‘πŸ‘
Loved your way of writing.

Collapse
 
nirbhayvashisht profile image
Nirbhay Vashisht

Thanks for the feedback AshwinπŸ˜„πŸ™
I am glad you liked it

Collapse
 
gsin11 profile image
Gurpreet Singh

Nice post Nirbhay, I have a created a cheat sheet for interns. Usually I handover it to them when they join the company.

GitHub logo gsin11 / git-cheat-sheet

GIT commands cheat sheet

GIT Commands Cheat Sheet

Project is about GIT commands

CREATE
Clone an existing repository
  $ git clone ssh://user@domain.com/repo.git
Create a new local repository
  $ git init
LOCAL CHANGES
Changed files in your working directory
  $ git status
Changes to tracked files
  $ git diff
Add all current changes to the next commit
  $ git add .

Add some changes in <file> to the next commit
  $ git add -p <file>

Commit all local changes in tracked files
  $ git commit -a
  $ git init

Commit previously staged changes
  $ git commit

Change the last commit
  $ git commit --amend
COMMIT HISTORY
Show all commits, starting with newest
  $ git log

Show changes over time for a specific file
  $ git log -p <file>

Who changed what and when in <file>
  $ git blame <file>
BRANCHES & TAGS
List all existing branches
  $ git branch -av

Fetch all origin branches on
…
Collapse
 
nirbhayvashisht profile image
Nirbhay Vashisht

Thank you very much for sharing this resource with us Gurpreet. Much Appreciated :D

Collapse
 
adela1905 profile image
adela1905

What a story, just awesome...

Collapse
 
nirbhayvashisht profile image
Nirbhay Vashisht

Thank you for the feedback :D

Collapse
 
thisismanaswini profile image
Manaswini

Awesome Nirbhay πŸ‘πŸ‘ I love the analogies! They explain stuff so much better. I am a fan of the images tooπŸ˜‚

Collapse
 
nirbhayvashisht profile image
Nirbhay Vashisht

Thanks ManaswiniπŸ˜„
I am glad you liked it😊

Collapse
 
chriswilkinsoncodes profile image
chris wilkinson

Congrats on submitting your first article! Lots of useful info here. Well done!!

Collapse
 
nirbhayvashisht profile image
Nirbhay Vashisht

Thank you for the feedback! Means a lot to me πŸ˜„

Collapse
 
rushijagani profile image
Rushi Jagani

Good job πŸ‘

Collapse
 
nirbhayvashisht profile image
Nirbhay Vashisht

Thanks for the feedbackπŸ˜„

Collapse
 
ozakaran profile image
πŸ†– Karan Oza

It's very easy and quite good article for the beginners.
Simple and easy way of explainations good use of pics and metaphorsπŸ‘
I will definitely recommend to my fellow buddies who wants to learn Git and Github✌️

Collapse
 
jrdev_ profile image
Jr. Dev πŸ‘¨πŸΎβ€πŸ’»

Good to see you on here Nirbhay. Nice article πŸ‘

Collapse
 
nirbhayvashisht profile image
Nirbhay Vashisht

Thank You very much for taking your time to read it and giving your feedback. Means a lot to me πŸ˜„

Collapse
 
poomansukhani5 profile image
Pooja Mansukhani πŸ’»

Very good article. I like the way you expressed in a very simple way.
πŸ€žπŸ»πŸ˜ŠπŸ‘πŸ»πŸ‘πŸ»

Collapse
 
nirbhayvashisht profile image
Nirbhay Vashisht

Thank you very much for the feedback πŸ˜„