git version control25 min

Git Basics

Understanding how Git tracks and records changes to your code

0/9Not Started

Why This Matters

Every software project needs a way to track changes. Without version control, you are left renaming files like project-final-v2-REALLY-FINAL.zip and hoping nothing breaks. Git solves this by recording every change you make, who made it, and why -- creating a complete history of your project that you can navigate, search, and undo at will.

Git is the most widely used version control system in the world. It powers collaboration at every scale, from solo side projects to codebases with thousands of contributors. Understanding Git fundamentals -- how a repository stores data, how a commit captures a snapshot, and how the staging area lets you control exactly what goes into each commit -- is essential knowledge for every developer.

Whether you are writing your first program or contributing to open source, Git is the tool that keeps your work safe and your history clean.

Define Terms

Visual Model

Working DirectoryEdit your files here
Staging AreaStage changes for commit
RepositoryPermanent commit history
git add
git commit
git status
git log
git add
git commit

The full process at a glance. Click Start tour to walk through each step.

The three areas of Git: working directory, staging area, and repository. Changes flow left to right.

Code Example

Code
// Initialize a new Git repository
// Run these commands in your terminal

// 1. Create a project folder and initialize Git
// $ mkdir my-project && cd my-project
// $ git init

// 2. Create a file and check status
// $ echo "Hello Git" > readme.txt
// $ git status
// Output: Untracked files: readme.txt

// 3. Stage the file
// $ git add readme.txt
// $ git status
// Output: Changes to be committed: new file: readme.txt

// 4. Commit with a message
// $ git commit -m "Add readme file"

// 5. View the commit history
// $ git log --oneline
// Output: a1b2c3d Add readme file

// 6. Make a change and see the diff
// $ echo "More content" >> readme.txt
// $ git diff
// Output: +More content

// Simulating the workflow in JavaScript
class SimpleGit {
  constructor() {
    this.workingDir = {};
    this.stagingArea = {};
    this.commits = [];
  }

  editFile(name, content) {
    this.workingDir[name] = content;
  }

  add(name) {
    if (!(name in this.workingDir)) {
      throw new Error("File not found: " + name);
    }
    this.stagingArea[name] = this.workingDir[name];
  }

  commit(message) {
    if (Object.keys(this.stagingArea).length === 0) {
      throw new Error("Nothing to commit");
    }
    this.commits.push({
      message,
      files: { ...this.stagingArea },
      timestamp: new Date().toISOString()
    });
    this.stagingArea = {};
  }

  log() {
    return this.commits.map(
      (c, i) => i + ": " + c.message
    ).join("\n");
  }
}

const git = new SimpleGit();
git.editFile("readme.txt", "Hello Git");
git.add("readme.txt");
git.commit("Add readme file");
console.log(git.log());

Interactive Experiment

Try these exercises in a real terminal:

  • Run git init in a new folder. Then run ls -la -- can you find the hidden .git directory?
  • Create three files, but only git add two of them. Run git status -- which file is listed as untracked?
  • Make a commit, then edit one of the files. Run git diff to see exactly what changed line by line.
  • Run git log --oneline after multiple commits. Notice how each commit has a unique short hash.

Quick Quiz

Coding Challenge

Simple Version Control System

Build a `VersionControl` class that simulates basic Git operations. It should support: `editFile(name, content)` to modify a file in the working directory, `add(name)` to stage a file, `commit(message)` to save staged files as a snapshot (throw an error with message 'Nothing to commit' if staging area is empty), `log()` to return an array of commit message strings, and `getFile(name)` to return the current content from the working directory (or throw 'File not found').

Loading editor...

Real-World Usage

Git is the backbone of modern software development:

  • Solo developers use Git to track their own changes, experiment safely, and undo mistakes with git log and git diff.
  • Open source projects like Linux, React, and VS Code use Git to coordinate thousands of contributors across time zones.
  • Companies rely on Git for code review, deployment pipelines, and audit trails -- every change is recorded with who made it and why.
  • Writers and researchers use Git to track versions of papers, manuscripts, and data analysis scripts.

The init, add, commit, status, log, and diff commands you learned here are the ones you will use every single day as a developer.

Connections