Git
Distributed version control system created by Linus Torvalds in 2005. Foundation of every modern development workflow — from local commits to global collaboration.
Git is a distributed version control system where every clone is a complete repository with full history. It doesn't depend on a central server to function — you can commit, branch, and merge completely offline.
Why it matters
Git is the invisible infrastructure on which all modern software development is built. Every developer has a complete copy of the history, branching and merging are millisecond operations, and cryptographic integrity ensures every commit verifies all its content and ancestors. GitHub, GitLab, Bitbucket, and Azure DevOps — all are built on Git. Understanding its internal model (objects, refs, commit DAG) is what separates someone who uses Git from someone who truly masters it.
Mental model
Working Directory → Staging Area (Index) → Local Repository → Remote Repository
edit git add git commit git push
Git's three trees:
- Working tree — the files you see on disk
- Index (staging area) — what will go in the next commit
- HEAD — the last commit of the current branch
Essential commands with examples
Initial setup
# Identity (required before first commit)
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# Default editor
git config --global core.editor "code --wait"
# View all active configuration
git config --list --show-origin
# Useful aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all --decorate"Create and clone
# New repository
git init my-project
cd my-project
# Clone existing
git clone https://github.com/user/repo.git
# Clone only latest revision (fast for CI)
git clone --depth 1 https://github.com/user/repo.git
# Clone specific branch
git clone -b develop https://github.com/user/repo.gitStaging and commits
# View current status
git status
git status -s # short format
# Add files to staging
git add file.ts # specific file
git add src/ # entire directory
git add -A # everything (new, modified, deleted)
git add -p # interactive: choose individual hunks
# Remove from staging (without losing changes)
git restore --staged file.ts # Git 2.23+
git reset HEAD file.ts # classic way
# Commit
git commit -m "feat: add user authentication"
git commit -am "fix: typo in header" # add + commit (tracked files only)
git commit --amend # modify last commit
git commit --amend --no-edit # same message, add forgotten files
git commit --allow-empty -m "trigger CI" # empty commitBranches
# List branches
git branch # local
git branch -r # remote
git branch -a # all
git branch -v # with last commit
# Create and switch
git branch feature/login
git checkout feature/login
git checkout -b feature/login # create + switch in one step
git switch -c feature/login # Git 2.23+ (preferred)
# Rename
git branch -m old-name new-name
git branch -m new-name # rename current branch
# Delete
git branch -d feature/login # only if already merged
git branch -D feature/login # force delete
git push origin --delete feature/login # delete remote branchMerge and rebase
# Merge: incorporate changes from another branch
git checkout main
git merge feature/login # merge commit (no fast-forward if diverged)
git merge --no-ff feature/login # force merge commit (useful for history)
git merge --squash feature/login # squash all commits into one
git merge --abort # cancel merge with conflicts
# Rebase: rewrite history on top of another branch
git checkout feature/login
git rebase main # move commits on top of main
git rebase -i HEAD~3 # interactive rebase (last 3 commits)
git rebase --abort # cancel rebase
git rebase --continue # after resolving conflicts
# Interactive rebase — options per commit:
# pick = use commit as-is
# reword = change message
# edit = pause to modify
# squash = combine with previous
# fixup = combine without message
# drop = delete commitHistory inspection
# Basic log
git log
git log --oneline # one line per commit
git log --oneline --graph --all # visual graph of all branches
git log --since="2 weeks ago"
git log --author="name"
git log -- file.ts # history of a file
git log -p -- file.ts # with diffs
# Search history
git log --grep="fix" # commits whose message contains "fix"
git log -S "functionName" # commits that added/removed that text
git log -G "regex" # regex search in diffs
# Differences
git diff # working tree vs staging
git diff --staged # staging vs last commit
git diff main..feature/login # between two branches
git diff HEAD~3..HEAD # last 3 commits
git diff --stat # summary of changed files
git diff --name-only # file names only
# Who wrote each line
git blame file.ts
git blame -L 10,20 file.ts # only lines 10-20
# Show a specific commit
git show abc1234
git show HEAD~2:file.ts # file at a previous commitStash
# Save changes temporarily
git stash # stash with automatic message
git stash push -m "work in progress" # with descriptive message
git stash push -p # interactive stash (choose hunks)
git stash --include-untracked # include new files
# Recover
git stash pop # apply and remove from stash
git stash apply # apply without removing
git stash apply stash@{2} # apply a specific stash
# Manage
git stash list # view all stashes
git stash show -p stash@{0} # view stash contents
git stash drop stash@{0} # delete a stash
git stash clear # delete allRemotes
# View configured remotes
git remote -v
# Add remote
git remote add upstream https://github.com/original/repo.git
# Fetch changes without merge
git fetch origin
git fetch --all # from all remotes
git fetch --prune # remove refs to deleted remote branches
# Fetch and merge
git pull # fetch + merge
git pull --rebase # fetch + rebase (linear history)
git pull --rebase --autostash # automatic stash before rebase
# Push changes
git push origin main
git push -u origin feature/login # push + set upstream
git push --force-with-lease # safe force push (verifies no one else pushed)
git push origin --tags # push all tagsUndoing changes
# Discard changes in working tree
git restore file.ts # Git 2.23+
git checkout -- file.ts # classic way
# Undo last commit (keep changes)
git reset --soft HEAD~1 # changes stay in staging
git reset --mixed HEAD~1 # changes stay in working tree (default)
git reset --hard HEAD~1 # ⚠️ delete changes completely
# Revert a commit (creates new inverse commit — safe for shared branches)
git revert abc1234
git revert HEAD # revert last commit
git revert --no-commit HEAD~3..HEAD # revert range without individual commits
# Recover "lost" commits
git reflog # history of HEAD movements
git checkout abc1234 # go to a reflog commit
git branch recovery abc1234 # create branch from recovered commitTags
# Create
git tag v1.0.0 # lightweight tag
git tag -a v1.0.0 -m "Release 1.0.0" # annotated tag (recommended)
git tag -a v1.0.0 abc1234 # tag at specific commit
# List and view
git tag
git tag -l "v1.*" # filter by pattern
git show v1.0.0
# Publish
git push origin v1.0.0 # one tag
git push origin --tags # all tags
# Delete
git tag -d v1.0.0 # local
git push origin --delete v1.0.0 # remoteCherry-pick and bisect
# Apply a specific commit to current branch
git cherry-pick abc1234
git cherry-pick abc1234 def5678 # multiple commits
git cherry-pick --no-commit abc1234 # apply without committing
# Find the commit that introduced a bug (binary search)
git bisect start
git bisect bad # current commit has the bug
git bisect good v1.0.0 # this commit was fine
# Git takes you to intermediate commits — mark good/bad until you find the culprit
git bisect reset # return to normal state
# Automatic bisect with a test script
git bisect start HEAD v1.0.0
git bisect run npm testCleanup and maintenance
# Remove untracked files
git clean -n # dry run (see what would be deleted)
git clean -fd # delete files and directories
git clean -fdx # include files ignored by .gitignore
# Optimize repository
git gc # garbage collection
git gc --aggressive # more aggressive compression
# Verify integrity
git fsck.gitignore
# Common patterns
node_modules/
dist/
.env
.env.local
*.log
.DS_Store
.next/
coverage/Key rule: never commit secrets. If a secret reaches the history, rotate it immediately — git filter-branch or BFG Repo-Cleaner can remove it from history, but assume it's already compromised.
References
- Pro Git Book — Scott Chacon & Ben Straub, 2014. The definitive book, free and official.
- Git Reference — Git Project. Official documentation for every command.
- Oh Shit, Git!?! — Katie Sylor-Miller. Practical guide for getting out of common situations.
- Atlassian Git Tutorials — Atlassian, 2024. Visual tutorials with clear explanations.
- Git Flight Rules — Kate Hudson, 2023. Recipes for specific scenarios NASA "flight rules" style.