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.
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.
Working Directory → Staging Area (Index) → Local Repository → Remote Repository
edit git add git commit git push
Git's three trees:
# 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"# 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.git# 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 commit# 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 branch# 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 commit# 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 commit# 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 all# 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 tags# 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 commit# 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 # remote# 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 test# 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# 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.
Code organization strategy where multiple projects coexist in a single repository, sharing dependencies, configuration, and build tooling.
Collaborative development platform built on Git. More than repository hosting — it's the central hub for code review, CI/CD, project management, and open source collaboration.
Branching model for Git proposed by Vincent Driessen in 2010. Defines branches with fixed roles (main, develop, feature, release, hotfix) for managing structured releases.
Minimalist branching model designed for continuous deployment. Only two elements — main and feature branches — with PRs as the integration point and immediate deploy after merge.