Essential Git Commands
The daily-driver commands every developer uses - staging, committing, branching, and inspecting history.
Search across all documentation pages
The daily-driver commands every developer uses - staging, committing, branching, and inspecting history.
🤖 Read the SystemsArchitect.io Blog for over 100+ cloud architecture articles 🔥
Quick-reference recipe card - copy-paste ready.
# Initialize a new repo
git init
# Check current status
git status
# Stage specific files
git add file1.tsx file2.tsx
# Stage all changes
git add .
# Commit with message
git commit -m "feat: add user profile component"
# View commit history
git log --oneline --graph
# Create and switch to a new branch
git checkout -b feature/new-component
# Switch to existing branch
git checkout main
# or (modern)
git switch mainWhen to reach for this: Every time you write code. These commands are the foundation of all Git workflows.
# Typical feature development workflow
git checkout -b feature/auth-page
# ... write code ...
git status
git add src/app/auth/page.tsx src/app/auth/layout.tsx
git commit -m "feat: add authentication page with login form"
git push -u origin feature/auth-pageWhat this demonstrates:
git add . when possible)The staging area is Git's unique intermediate step between your working directory and the repository.
# Stage specific files
git add src/components/Button.tsx
# Stage parts of a file (interactive hunk selection)
git add -p src/components/Button.tsx
# Unstage a file (keep changes in working directory)
git restore --staged src/components/Button.tsx
# Discard changes in working directory
git restore src/components/Button.tsx# Conventional commit prefixes
git commit -m "feat: add dark mode toggle"
git commit -m "fix: resolve hydration mismatch in sidebar"
git commit -m "refactor: extract shared form validation logic"
git commit -m "docs: update API route documentation"
git commit -m "chore: upgrade next to 15.1.0"
git commit -m "test: add integration tests for checkout flow"
# Multi-line commit message
git commit -m "feat: add user dashboard
- Add profile summary card
- Add recent activity feed
- Add quick action buttons"# Compact log with graph
git log --oneline --graph --all
# Show changes in a specific commit
git show abc1234
# Show who last modified each line
git blame src/app/layout.tsx
# Search commit messages
git log --grep="auth" --oneline
# Show commits that changed a specific file
git log --oneline -- src/app/auth/page.tsx
# Show diff between branches
git diff main..feature/auth-page# List all branches (local)
git branch
# List all branches (including remote)
git branch -a
# Create a branch without switching
git branch feature/sidebar
# Delete a merged branch
git branch -d feature/sidebar
# Delete an unmerged branch (force)
git branch -D feature/abandoned
# Rename current branch
git branch -m new-nameThings that will bite you. Each gotcha includes what goes wrong, why it happens, and the fix.
Committing to the wrong branch - You write code on main instead of a feature branch. Fix: git stash, then git checkout -b feature/correct-branch, then git stash pop.
Forgetting to pull before pushing - Push is rejected because remote has new commits. Fix: git pull --rebase to replay your commits on top of the latest remote changes.
Accidentally staging secrets - .env or credentials get added to staging. Fix: git restore --staged .env before committing. Add .env to .gitignore immediately.
Detached HEAD state - You checked out a commit hash instead of a branch name. Fix: git checkout -b my-branch to create a branch at that commit, or git checkout main to go back.
Large files bloating the repo - Committing node_modules or build artifacts. Fix: Add them to .gitignore before the first commit. If already committed, use git rm -r --cached node_modules.
Other ways to solve the same problem - and when each is the better choice.
| Alternative | Use When | Don't Use When |
|---|---|---|
git switch / git restore | Modern Git (2.23+) - clearer intent than checkout | Older Git versions or muscle memory |
git stash | Quick context switch without committing WIP | You want a permanent record of the WIP state |
git worktree | Working on two branches simultaneously | Simple single-branch workflows |
git add . stages all changes in the current directory and subdirectoriesgit add file.tsx stages only that specific file.env files)git switch was introduced in Git 2.23+ to clarify branch-switching intentgit checkout is overloaded: it switches branches AND restores filesgit switch for branches, git restore for files, if your Git version supports themgit reset --soft HEAD~1--soft keeps changes staged--mixed (default) keeps changes but unstages them--hard discards changes entirely (dangerous)feat:, fix:, refactor:, docs:, chore:, test: categorize changesgit show abc1234--stat for a summary of changed files instead of the full diffgit filter-branch or git filter-repo to remove the file from history.env to .gitignore to prevent future accidentsgit add -p only works on tracked files (files already known to Git)git add file.tsx, then you can use -p on subsequent changesgit branch -d feature/my-branch-d only deletes if the branch has been merged-D force-deletes even unmerged branches (use with caution)git checkout -b my-new-branch to create a branch at the current commitgit log --grep="auth" --oneline--grep filters by commit message content--all to search across all branches*.tsbuildinfo to your .gitignorenpx tsc --noEmit--noEmit checks types without producing output filesReviewed by Chris St. John·Last updated Jul 19, 2026
🤖 Read the SystemsArchitect.io Blog for over 100+ cloud architecture articles 🔥