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.
A distributed version-control system for every project size
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 .
Get Git running on your platform
macOS (Homebrew): brew install git
Ubuntu / Debian: sudo apt-get install git
Windows: Download the installer from git-scm.com and follow the wizard.
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.
Commits, staging, branches and the object database
Working Directory → Index → Repository — Edit files, stage changes, then commit snapshots.
Everything is a blob, tree, commit or tag. Objects are content-addressed by SHA-1 or SHA-256 (experimental) hashes, guaranteeing integrity.
A branch is a movable pointer to a commit. HEAD
is your current checkout.
Since Git 2.45 you can opt-in to the reftable backend for faster reference look-ups: git init --ref-format=reftable
.
Status → Add → Commit → Log
Command | Description | Syntax Example |
---|---|---|
git status | Show staged / unstaged changes | git status |
git add | Stage files | git add README.md |
git commit | Record snapshot | git commit -m "feat: initial" |
git log | View history | git log --oneline --graph |
Create new repo: git init
Clone remote: git clone https://example.com/project.git
Diff working vs index: git diff
Diff index vs HEAD: git diff --cached
Parallel development the Git way
Create branch: git branch feature/login
Switch (≥Git 2.23): git switch feature/login
Fast-forward: git merge feature/login
Three-way merge when histories diverge. Conflicts appear in-file; resolve then git add and commit.
Linearize history: git rebase main
Notes: Never rebase commits that others have already pulled.
Apply single commit: git cherry-pick <sha>
Undo with new commit: git revert <sha>
Push, fetch, pull & review
Add remote: git remote add origin git@github.com:user/repo.git
Rename: git remote rename origin production
Fetch: git fetch
downloads refs, no merge.
Pull: git pull --rebase
fetch + rebase for cleaner history.
Push: git push -u origin main
git checkout -b feature/ui --track origin/feature/ui
Reflog, reset, bisect & more
View all head movements: git reflog
. Recover deleted branches by resetting to a reflog entry.
Soft: keep index; Mixed (default): keep working dir; Hard: discard everything.
git reset --hard HEAD~1
Binary search for a bad commit:
git bisect start
git bisect bad
git bisect good v1.2.0
Temporary shelves and version markers
Save work: git stash push -m "wip: fix bug"
Apply latest: git stash pop
Lightweight: git tag v1.0.0
Annotated (signed): git tag -a v1.0.0 -m "First stable" -s
Custom scripts for quality gates & CI
Place executable scripts in .git/hooks/
(e.g., pre-commit
, pre-push
).
Enforce policies like branch-naming or commit message linting on the remote.
Partial clone, sparse checkout & Scalar
Download only referenced objects: git clone --filter=blob:none --depth=1 url
. Great for monorepos .
Limit working directory to specific paths:
git sparse-checkout init --cone
git sparse-checkout set src/ docs/
Microsoft’s Scalar enables background prefetch, partial clone defaults and repo maintenance for massive projects .
Track large binaries outside the main object store: git lfs track "*.psd"
followed by commit and push.
Signing, credential storage & certificate auth
GPG: git commit -S -m "secure"
X.509 (S/MIME): git config --global gpg.format x509
Cache for session: git config --global credential.helper cache
macOS Keychain: git config --global credential.helper osxkeychain
Git 2.44 added first-class certificate authentication for enterprises .
Highlights from Git 2.44 & 2.45
Experimental backend for references, improving performance on repos with many branches .
Prepare for stronger hashing; mixed SHA-1/SHA-256 repos now interoperable (opt-in).
Commands like git sparse-checkout reconfigure
simplify path-set updates.
Certificate auth, default protected configs, clearer advice messages and improved git pull conflict hints .
From solo projects to enterprise CI
Develop each feature on its own branch, merge via pull request, protect main.
Short-lived branches, continuous deployment, squash-merge for tidy history.
Separate develop, release, hotfix branches; suited to versioned products.
Commit small increments directly to main behind feature flags; ideal for micro-services.
Manuals, books & tutorials
git-scm.com/docs — exhaustive command manual.
The freely available book by Chacon & Straub covers everything from basics to internals.
Web-based sandboxes such as learngitbranching.js.org let you practice without risk.
From scaffold to feature merge with CMake
Directory layout:
cpp-app/
src/ main.cpp
include/ utils.hpp
CMakeLists.txt
git init
git add .
git commit -m "chore: bootstrap C++ app"
echo -e "build/\n*.o\n*.exe" >> .gitignore
git add .gitignore
git commit -m "chore: add ignore rules"
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"
Static-site development with live-reload
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"
echo -e "dist/\nnode_modules/\n.env" >> .gitignore
git add .gitignore
git commit -m "chore: ignore build output"
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