Master the essential Git commands that cover everything from daily workflow to fixing mistakes and collaborating with teams.
Git is the backbone of modern software development. Whether you work alone or in a team of 200, understanding Git at a deep level is what separates developers who lose work and create merge chaos from those who ship confidently.
This guide covers 20 commands — not just their syntax, but when and why to use them.
git status
Your first command every morning. Shows modified files, staged changes, and untracked files at a glance.
git add src/components/Button.tsx # specific file
git add src/ # whole directory
git add -p # interactive staging (best practice)
The -p flag lets you stage changes hunk by hunk — perfect for keeping commits focused.
git commit -m "feat: add dark mode toggle to settings"
Write commits in the imperative mood. Use Conventional Commits format (feat:, fix:, docs:) for clean changelogs.
git push origin main
git pull origin main --rebase # cleaner history than merge
git branch # list local branches
git branch feature/login # create new branch
git branch -d feature/login # delete merged branch
git branch -D feature/login # force delete
git switch main # modern syntax
git switch -c feature/auth # create and switch
git merge feature/login --no-ff # preserves merge commit
git rebase main # replay your branch on top of main
Use rebase for a linear history. Use merge for preserving branch topology.
git restore src/app.ts # discard working directory changes
git restore --staged src/app.ts # unstage a file
git reset HEAD~1 # undo last commit, keep changes staged
git reset --soft HEAD~1 # keep changes staged
git reset --hard HEAD~1 # discard everything — use carefully
git revert abc1234 # create a new commit that undoes abc1234
Always prefer revert over reset on shared branches.
git log --oneline --graph --all # beautiful visual history
git log -p src/auth.ts # history of a specific file
git diff # unstaged changes
git diff --staged # staged changes
git diff main feature/auth # between branches
git blame src/auth.ts # who wrote each line and when
git show abc1234 # full details of a specific commit
git stash push -m "wip: half-done auth"
git stash list
git stash pop
Stash is your escape hatch when you need to context-switch without committing.
git cherry-pick abc1234 # apply a specific commit to current branch
git bisect start
git bisect bad # current commit is broken
git bisect good v2.0 # this tag was working
Binary search through commits to find exactly which one introduced a bug.
git reflog # full history of HEAD movements — your safety net
If you accidentally deleted a branch or did a hard reset, reflog can get it back.
git tag v1.2.0 -m "Release 1.2.0"
git push origin --tags
These 20 commands cover 95% of everything you'll do with Git day to day. The ones that separate good developers from great ones are rebase, bisect, reflog, and the interactive staging with -p. Practice them in a test repo until they're muscle memory.