Git is a free, open-source version-control tool that lets you save snapshots of your project’s files as you work, switch to any earlier snapshot at any time, and safely collaborate with others without overwriting each other’s changes.

0. Introduction

A distributed version-control system for every project size

Why Git?

Git is a fast, distributed version-control system that records snapshots (commits) of your project, enabling parallel development through lightweight branching and allowing complete history on every clone .

1. Installation & Setup

Get Git running on your platform

1.1 Install

macOS (Homebrew): brew install git

Ubuntu / Debian: sudo apt-get install git

Windows: Download the installer from git-scm.com and follow the wizard.

1.2 Initial Configuration

git config --global user.name "Jane Developer"
git config --global user.email "jane@example.com"
git config --global init.defaultBranch main
git config --global core.editor "nano -w"

Notes: init.defaultBranch replaces the legacy master branch name for new repos.

2. Core Concepts

Commits, staging, branches and the object database

2.1 The Three-Tree Architecture

Working Directory → Index → Repository  — Edit files, stage changes, then commit snapshots.

2.2 Objects

Everything is a blob, tree, commit or tag. Objects are content-addressed by SHA-1 or SHA-256 (experimental) hashes, guaranteeing integrity.

2.3 Branches & HEAD

A branch is a movable pointer to a commit. HEAD is your current checkout.

2.4 References Storage

Since Git 2.45 you can opt-in to the reftable backend for faster reference look-ups: git init --ref-format=reftable .

3. Everyday Commands

Status → Add → Commit → Log

3.1 Quick Reference Table

CommandDescriptionSyntax Example
git statusShow staged / unstaged changesgit status
git addStage filesgit add README.md
git commitRecord snapshotgit commit -m "feat: initial"
git logView historygit log --oneline --graph

3.2 Cloning & Creating Repositories

Create new repo: git init

Clone remote: git clone https://example.com/project.git

3.3 Viewing Changes

Diff working vs index: git diff

Diff index vs HEAD: git diff --cached

4. Branching & Merging

Parallel development the Git way

4.1 Create & Switch

Create branch: git branch feature/login

Switch (≥Git 2.23): git switch feature/login

4.2 Merge

Fast-forward: git merge feature/login

Three-way merge when histories diverge. Conflicts appear in-file; resolve then git add and commit.

4.3 Rebase

Linearize history: git rebase main

Notes: Never rebase commits that others have already pulled.

4.4 Cherry-Pick & Revert

Apply single commit: git cherry-pick <sha>

Undo with new commit: git revert <sha>

5. Remote Collaboration

Push, fetch, pull & review

5.1 Remotes

Add remote: git remote add origin git@github.com:user/repo.git

Rename: git remote rename origin production

5.2 Synchronizing

Fetch: git fetch downloads refs, no merge.

Pull: git pull --rebase fetch + rebase for cleaner history.

Push: git push -u origin main

5.3 Tracking Branches

git checkout -b feature/ui --track origin/feature/ui

6. Undo & Troubleshooting

Reflog, reset, bisect & more

6.1 Reflog

View all head movements: git reflog. Recover deleted branches by resetting to a reflog entry.

6.2 Reset

Soft: keep index; Mixed (default): keep working dir; Hard: discard everything.

git reset --hard HEAD~1

6.3 Bisect

Binary search for a bad commit:

git bisect start
git bisect bad
git bisect good v1.2.0

7. Stashing, Tags & Releases

Temporary shelves and version markers

7.1 Stash

Save work: git stash push -m "wip: fix bug"

Apply latest: git stash pop

7.2 Tagging

Lightweight: git tag v1.0.0

Annotated (signed): git tag -a v1.0.0 -m "First stable" -s

8. Hooks & Automation

Custom scripts for quality gates & CI

8.1 Local Hooks

Place executable scripts in .git/hooks/ (e.g., pre-commit, pre-push).

8.2 Server-Side Hooks

Enforce policies like branch-naming or commit message linting on the remote.

9. Scaling Large Repositories

Partial clone, sparse checkout & Scalar

9.1 Partial Clone

Download only referenced objects: git clone --filter=blob:none --depth=1 url. Great for monorepos .

9.2 Sparse Checkout

Limit working directory to specific paths:

git sparse-checkout init --cone
git sparse-checkout set src/ docs/

9.3 Scalar

Microsoft’s Scalar enables background prefetch, partial clone defaults and repo maintenance for massive projects .

9.4 Git LFS

Track large binaries outside the main object store: git lfs track "*.psd" followed by commit and push.

10. Security Enhancements

Signing, credential storage & certificate auth

10.1 Signed Commits & Tags

GPG: git commit -S -m "secure"

X.509 (S/MIME): git config --global gpg.format x509

10.2 Credential Helpers

Cache for session: git config --global credential.helper cache

macOS Keychain: git config --global credential.helper osxkeychain

10.3 Certificate-Based Auth

Git 2.44 added first-class certificate authentication for enterprises .

11. Latest Features (2024-2025)

Highlights from Git 2.44 & 2.45

11.1 Reftable Backend

Experimental backend for references, improving performance on repos with many branches .

11.2 SHA-256 Interoperability

Prepare for stronger hashing; mixed SHA-1/SHA-256 repos now interoperable (opt-in).

11.3 Enhanced Sparse-Checkout UI

Commands like git sparse-checkout reconfigure simplify path-set updates.

11.4 Security & UX

Certificate auth, default protected configs, clearer advice messages and improved git pull conflict hints .

12. Best-Practice Workflows

From solo projects to enterprise CI

12.1 Feature Branch Flow

Develop each feature on its own branch, merge via pull request, protect main.

12.2 GitHub Flow

Short-lived branches, continuous deployment, squash-merge for tidy history.

12.3 Gitflow (NVIE)

Separate develop, release, hotfix branches; suited to versioned products.

12.4 Trunk-Based Development

Commit small increments directly to main behind feature flags; ideal for micro-services.

13. Further Reading

Manuals, books & tutorials

13.1 Official Docs

git-scm.com/docs — exhaustive command manual.

13.2 Pro Git Book

The freely available book by Chacon & Straub covers everything from basics to internals.

13.3 Interactive Tutorials

Web-based sandboxes such as learngitbranching.js.org let you practice without risk.

14. Example Workflow: C++ Project

From scaffold to feature merge with CMake

14.1 Scaffold & First Commit

Directory layout:

cpp-app/
  src/ main.cpp
  include/ utils.hpp
  CMakeLists.txt

git init
git add .
git commit -m "chore: bootstrap C++ app"

14.2 Ignore Build Artifacts

echo -e "build/\n*.o\n*.exe" >> .gitignore

git add .gitignore
git commit -m "chore: add ignore rules"

14.3 Feature Branch Example

git switch -c feature/cli-parser
# code changes …
git add src/cli.cpp include/cli.hpp
git commit -m "feat: add ArgParser"

Merge back to main after tests pass:

git switch main
git merge --no-ff feature/cli-parser -m "feat: ArgParser"

15. Example Workflow: HTML + CSS + JS Project

Static-site development with live-reload

15.1 Initial Commit

Directory layout:

web-site/
  index.html
  css/ styles.css
  js/ main.js

git init
git add index.html css js
git commit -m "feat: landing page skeleton"

15.2 Ignore Generated Assets

echo -e "dist/\nnode_modules/\n.env" >> .gitignore

git add .gitignore
git commit -m "chore: ignore build output"

15.3 Iterative Development with Branches

git switch -c feature/hero-animation
# edit css/animations.css + js/scroll.js
git add css js
git commit -m "feat: add hero section animation"

Rebase onto the freshest main before opening a pull request:

git fetch origin
git rebase origin/main

Resolve conflicts if they occur, then push the branch:

git push -u origin feature/hero-animation