Version Control: Git & GitHub

The Time Machine
for Code

Git is a distributed version control system. It allows humans to track changes in computer files and coordinate work on those files among multiple people.

Think of it as a "save game" system for your project. You can save your progress (commit), create parallel universes to experiment (branch), and merge those universes back together. GitHub acts as the cloud storage for these histories.

00 / The Origins

A Bit of History

Git was born out of frustration. In 2005, the relationship between the Linux kernel community and their previous VCS provider (BitKeeper) broke down.

Linus's Criteria

  • Speed (Fast enough to handle the Linux Kernel).
  • Simple design.
  • Strong support for non-linear development (thousands of parallel branches).
  • Fully distributed (no central server required).
"I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'Git'."

— Linus Torvalds

(British slang for "unpleasant person")

00.5 / Installation

Getting Started

Git works everywhere. Select your operating system to see specific installation instructions.

1. Download Git Bash

For Windows, we strongly recommend Git Bash. It provides a standard Unix-like environment. Go to git-scm.com and download the installer.

Tip: During install, choose "Git from the command line and also from 3rd-party software".

2. Verify

git --version
git version 2.43.0.windows.1
01 / The Local Flow

The Three States

Git architecture is defined by three areas: the Working Directory (where you edit), the Staging Area (what you plan to save), and the Repository (history).

Working Dir
Staging Area
Repository (.git)
index.html Modified
Current Command
$ status
Edit the file to start. Then stage it, then commit it to history.
02 / Data Model

Snapshots, Not Deltas

Unlike other VCS systems which store changes as a list of file-based changes (deltas), Git thinks of its data more like a series of snapshots of a miniature filesystem.

The "Delta" Way

V1
File A
V2
+ Diff
V3
+ Diff

The Git Way (Snapshots)

C1
Full File A (Blob)
C2
Full File A'
C3
Full File A''
03 / Parallel Universes

Branching Logic

Branching means you diverge from the main line of development and continue to do work without messing with that main line.

System Ready. Initializing main branch...

Main vs. Master

They are aliases for the exact same thing: the default branch.
master: Legacy name (2005-2020).
main: Modern standard (2020+) for inclusive terminology.

Feature Branch Workflow

Think of feature branches as photocopies of your project. You scribble notes (commits) on the copy, and only when it's perfect do you staple (merge) it into the final report (main).

05 / Resolution

Merge Conflicts

When two branches modify the same lines of the same file, Git doesn't know which one is correct. It pauses the merge and asks you to resolve it.

<<<<<<< HEAD
const color = "blue";
=======
const color = "green";
>>>>>>> feature

Git inserted markers. Delete them and pick the code you want.

05.5 / Precision Tools

Cherry-pick & Rebase

Git Cherry-pick

Taking a single specific commit from one branch and applying it to another, without merging the whole branch.

C3
Feature
C3'
Main
git cherry-pick a1b2c3d

Git Rebase

Rewriting history by moving the base of your branch to the tip of another, creating a linear history.

git rebase main
06 / The Cloud

GitHub as Remote

Git is the tool running on your machine. GitHub is a hosting service for Git repositories.

git remote add origin https://github.com/user/repo.git
git push -u origin main
Enumerating objects: 5, done.
Writing objects: 100% (5/5), 420 bytes, done.
To https://github.com/user/repo.git
  * [new branch] main -> main
Local Repository ------- sync -------> Remote
07 / Workflow

Deployment Guide

1

Initialize & Commit

Turn your folder into a Git repository and save your first version.

git init
git add .
git commit -m "Initial commit"
2

Branch Naming

Ensure you are using the modern 'main' branch name.

git branch -M main
3

Link Remote

Connect your local folder to the empty repository you created on GitHub.

git remote add origin https://github.com/username/repo-name.git
4

Push to Cloud

Upload your code. The '-u' flag saves the connection for future pushes.

git push -u origin main
08 / Reference

Git Command Guide

Starting Out

git init

Initializes a new Git repository. Creates a .git folder.

git clone [url]

Copies an entire repository from a remote source (like GitHub) to your machine.

git config --global user.name

Sets your identity for commits.

Saving Changes

git status

Shows the state of the working directory and staging area.

git add [file]

Adds files to the staging area (index).

git commit -m "[msg]"

Saves the staged snapshot to the repository history.

Branching & Merging

git branch

Lists your branches. git branch [name] creates one.

git checkout [branch]

Switches to another branch.

git merge [branch]

Combines the specified branch's history into the current branch.

Sharing & Updating

git push

Uploads local branch commits to the remote repository.

git fetch

Downloads history from the remote but does not merge it.

git pull

Fetches and merges changes from the remote to your local branch.

git log

Shows the commit history.

Glossary

Key Definitions

Version Control System (VCS)

A system that records changes to a file or set of files over time so that you can recall specific versions later.

Git

A distributed version control system for tracking changes in source code during software development.

Repository (Repo)

A data structure used by a VCS to store metadata for a set of files and directories.

Commit

A snapshot of your entire project at a specific point in time.

Branch

A parallel version of a repository. It is contained within the repository, but does not affect the primary or master branch.

Staging Area (Index)

A file that maintains a list of files which are getting ready to be committed.

Merge

Taking the changes from one branch (or commit) and integrating them into another.

Clone

A copy of an existing Git repository.

Pull

Fetching from and integrating with another repository or a local branch.

Push

Uploading local repository content to a remote repository.

08 / Knowledge Check