iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Git Intro

Git is a distributed version control system. Every clone has the full history; branching is cheap; merging is explicit.

Git — what it is

EXAMPLE
# ===== The model =====
# Working tree -> Index (staging) -> Commit -> Branch -> Remote
# Each commit is a snapshot identified by a sha; commits link to parents -> a DAG
# Distributed: every clone is a complete repo (no central server required, just a convention)

# ===== Hello, git =====
git init my-app && cd my-app
echo 'hi' > README.md
git add README.md
git commit -m 'initial commit'

git remote add origin https://github.com/me/my-app.git
git push -u origin main

# ===== Daily loop =====
git status -sb                 # what changed, where am I?
git diff                       # unstaged
git diff --staged              # staged
git add -p                     # stage hunks interactively
git commit -m 'msg'
git push

# ===== Branches =====
git switch -c feature/orders   # new branch off current
git switch main                # back to main
git merge feature/orders       # bring changes in
git branch -d feature/orders   # safe delete

# ===== Pull, fetch, rebase =====
git fetch                      # download, don't merge
git pull --ff-only             # only fast-forward; refuse drift
git rebase main                # replay current branch onto main

# ===== History =====
git log --oneline --graph --decorate -20
git log -p src/handlers.ts     # patches that touched the file
git blame src/handlers.ts      # who changed each line, when

# ===== Undoing safely =====
git restore src/file.ts        # discard unstaged changes
git restore --staged file.ts   # unstage
git revert <sha>               # create a new commit that undoes <sha>
git reset --soft HEAD^         # remove last commit, keep changes staged
# git reset --hard is destructive; avoid unless you mean it

# ===== Tagging =====
git tag v1.0.0
git push --tags

# ===== When git wins =====
# - Any code project, solo or team
# - Offline work + later sync
# - Branch-and-merge workflows of any complexity

# ===== Patterns to internalise =====
# - Commit early, commit often, push when ready to share
# - Small focused commits beat large 'wip' commits
# - Branch names that describe intent (feature/orders, fix/login-throttle)
# - Read history with --graph; the topology tells the story

# ===== Pitfalls =====
# - Force-push to shared branches -> rewrites others' work
# - Committing secrets (.env) -> rotate immediately
# - Big binary files -> use Git LFS
# - 'I committed to the wrong branch' -> branch + cherry-pick + reset, don't panic

Why it matters

Git is the platform every team standardises on for a reason. The DAG model, cheap branching, and offline-first design make iteration safe. Memorise status + add + commit + log + diff + switch + merge + rebase, and the rest of git is just the same primitives in different combinations.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Git tracks the history of files in a project.
git init && git add . && git commit -m 'initial'
Try it Yourself »

Exercise

Create a new repository.

git

Test yourself

Q1. Git is best described as…
Q2. Initialise a new repo with…
Q3. Clone an existing repo with…

Discussion

Loading…