Git and GitHub · Part 1 · Git basics

What Git is and why it exists

The problem Git solves

Imagine a website that works and is live. A few changes later, meant to make it better, things break and the code is a mess. Nobody remembers exactly what changed, and everyone just wants the working version back.

Without Git, the usual answer is to keep copies of the folder:

  • website-final/
  • website-final-v2/
  • website-final-v2-REAL/
  • website-final-DONT-DELETE/

Which one is the real one? Nobody knows what changed between them, or why.

Copies like these don't say what changed or why, going back means digging through folders, and two people working at once overwrite each other. Git fixes all of that.

Like save points in a video game

Before a hard level, a player saves the game. If the level goes badly, they load the save and they're back where things were fine. Git does the same for code:

In a video gameIn Git
Save pointCommit
Name of the save (“Before boss fight”)Commit message (“Add login form”)
List of all savesHistory (git log)
Loading an old saveGoing back to an old commit
A second playthrough that tries a different pathBranch

What Git keeps track of

Every commit records what changed (files and lines), who changed it, when, and why, in the commit message. A history looks like this:

  1. Nov 23Nithin“Add contact form”
  2. Nov 22Nithin“Fix navbar on mobile”Worked
  3. Nov 21Nithin“Create homepage layout”
  4. Nov 20Nithin“Initial project setup”

If the contact form breaks the site, the project can go straight back to “Fix navbar on mobile.”

Two more reasons to use Git

  • It works offline. Git lives in the project folder. It needs no internet and no account.
  • It makes teamwork possible. Several people can work on the same project at once, and Git combines their work. When two people change the same lines, Git asks a person to choose. That's called a merge conflict.

Git is not GitHub

The two names get mixed up all the time.

GitGitHub
What it isA tool on the computer that takes snapshotsA website that stores Git projects online
What it's forTracking changes and historyBacking up, sharing and collaborating
Needs internetNoYes
Think of it asThe cameraGoogle Photos for the snapshots

Linus Torvalds created Git in 2005 to manage the Linux kernel. GitHub came in 2008, built on top of it. GitLab and Bitbucket are alternatives to GitHub that use the same Git underneath.