Git

Last edited by dave on 01/12/2025, 08:10:33 UTC

Git

Contents

Git Logo

Quick Snapshot

  • Type: Distributed version control system.
  • Created by: Linus Torvalds (yes, the same Linux guy).
  • First released: 2005.
  • Core idea: Track changes in code (or any text files) efficiently and collaboratively.
  • License: GNU General Public License (GPLv2).
  • Tagline (unofficial): “Because losing your code should never be an option.”
  • Website: git-scm.com

A Brief History

In 2005, the Linux kernel team needed a better way to manage massive amounts of source code.
They had tried commercial systems like BitKeeper, but when that relationship soured, Linus Torvalds built Git — a fast, distributed, and reliable alternative.

It quickly became the de facto standard for version control, now powering everything from tiny open-source projects to enterprise monorepos.


Core Concepts

Repositories (Repos)

A repository is a Git project — your code, its history, and all related metadata.
It can live locally on your computer or remotely (like on GitHub, GitLab, or Bitbucket).

git init my-project cd my-project

Boom — you just made a repo.


Commits

A commit is a snapshot of your project at a specific point in time.
Each commit has:

  • A unique hash (like a1b2c3d4)
  • An author
  • A timestamp
  • A message describing what changed
git add . git commit -m "Fix broken penguin emoji rendering"

Branches

Branches let you experiment without breaking the main codebase.
You can think of them as alternate timelines.

git checkout -b feature/sandwich-ai

Work freely, then merge your changes back when ready.


Merging & Rebasing

  • Merge: Combines two branches, preserving their histories.
  • Rebase: Moves your changes on top of another branch, keeping history linear.

Merging feels like collaboration; rebasing feels like time travel. Both are useful.

git merge main # or git rebase main

The Staging Area

Git tracks changes in three zones:

  1. Working Directory: Where you edit files.
  2. Staging Area: Where you prepare commits.
  3. Repository: Where your history lives.

You can check what’s where:

git status

Remote Repositories

To collaborate, you’ll push and pull code to a remote server:

git remote add origin https://github.com/user/repo.git git push -u origin main

Remote services (like GitHub) also enable pull requests, issue tracking, and CI/CD integration.


Common Commands

If you are looking for the ultimate cheat sheet for Git, check out Git cheat sheet.

CommandDescription
git initInitialize a repository
git clone <url>Copy a remote repo locally
git add <file>Stage changes
git commit -m "msg"Save staged changes
git statusSee what changed
git logView history
git diffCompare changes
git branchList or create branches
git checkout <branch>Switch branches
git merge <branch>Merge a branch into current
git pullFetch + merge remote updates
git pushUpload local commits to remote

The Distributed Magic

Unlike centralized systems, every Git clone is a complete copy of the repository, including history.
That means you can:

  • Work offline.
  • Recover everything, even if the main server disappears.
  • Experiment freely without fear.

.gitignore

Not every file belongs in version control.
The .gitignore file tells Git what to leave out — things like build artifacts, logs, and node_modules.

Example:

node_modules/
dist/
.env

Collaboration Workflow

Typical flow:

  1. Clone the repo.
  2. Create a branch for your feature.
  3. Commit your changes.
  4. Push to remote.
  5. Open a Pull Request (PR) for review.
  6. Merge when approved.

Clean, traceable, and everyone stays (mostly) sane.


Advanced Tools & Concepts

  • Tags: Mark specific commits, like releases.
    git tag v1.0.0
  • Stash: Temporarily save unfinished work.
    git stash git stash pop
  • Cherry-pick: Apply a specific commit to another branch.
    git cherry-pick <hash>
  • Hooks: Automate tasks on Git events (e.g., pre-commit linting).
  • Submodules: Track other repos inside your repo (use carefully).

Git Internals (In Brief)

Git is really a key-value store of snapshots.
Commits point to trees (directories), which point to blobs (file contents).
Everything is identified by its SHA-1 hash, ensuring integrity and speed.

In essence, Git isn’t just version control — it’s a time machine for text.


Ecosystem

  • GitHub — Social coding, PRs, issues, and collaboration.
  • GitLab — DevOps integration, CI/CD pipelines.
  • Bitbucket — Popular with enterprises and Atlassian users.
  • Gitea / Gogs — Lightweight self-hosted alternatives.

All built on the same Git foundation.


Fun Facts

  • Git’s name is British slang for “unpleasant person” — Linus’ own joke about himself.
  • The .git directory is the brain of your repo; delete it, and history literally dies.
  • The Linux kernel is still managed with Git, by thousands of developers worldwide.
  • You can store poems, notes, or recipes in Git. (Some people actually do.)
Backlinks (3)
Categories (0)

    No categories assigned to this page.

Edit Level

> Signed In Users