20 Git Commands Every Developer Should Know
Git is the version control system used by virtually every professional software team on the planet. Whether you're working solo or collaborating with a large team, mastering Git is non-negotiable. This guide covers 20 commands — organized by category — that you'll reach for day after day.
Setup & Configuration
1. git config
Set your identity before making your first commit:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
2. git init
Initialize a new Git repository in the current directory:
git init
3. git clone
Copy an existing repository to your local machine:
git clone https://github.com/user/repo.git
Everyday Workflow
4. git status
Check what files have changed, what's staged, and what's untracked. Run this constantly — it's your compass.
5. git add
Stage changes for the next commit. Use git add . to stage everything, or specify files individually:
git add src/app.js
6. git commit
Save a snapshot of your staged changes with a descriptive message:
git commit -m "feat: add user authentication endpoint"
7. git push
Upload your local commits to a remote repository:
git push origin main
8. git pull
Fetch and integrate changes from the remote into your current branch:
git pull origin main
Branching & Merging
9. git branch
List all branches, or create a new one:
git branch feature/login-page
10. git checkout / git switch
Switch to a different branch. Modern Git prefers git switch:
git switch feature/login-page
11. git merge
Integrate changes from one branch into the current branch:
git merge feature/login-page
12. git rebase
Reapply commits on top of another base, producing a cleaner linear history. Use carefully in shared branches:
git rebase main
Undoing Mistakes
13. git restore
Discard unstaged changes to a file (replaces the older git checkout -- file):
git restore src/app.js
14. git reset
Undo commits by moving the branch pointer. Use --soft to keep changes staged, --hard to discard them entirely:
git reset --soft HEAD~1
15. git revert
Create a new commit that undoes a previous one — the safe option for shared branches:
git revert a3f8c21
Inspecting History
16. git log
View commit history. Use --oneline --graph for a compact visual view:
git log --oneline --graph --all
17. git diff
Show differences between your working directory and the last commit, or between two commits or branches.
18. git blame
See who last modified each line of a file — useful for understanding context and history.
Advanced Utilities
19. git stash
Temporarily shelve your uncommitted changes so you can switch context, then restore them later:
git stash
git stash pop
20. git cherry-pick
Apply a specific commit from one branch onto another without merging the whole branch:
git cherry-pick a3f8c21
Quick Reference Table
| Category | Commands |
|---|---|
| Setup | config, init, clone |
| Daily Workflow | status, add, commit, push, pull |
| Branching | branch, switch, merge, rebase |
| Undoing | restore, reset, revert |
| Inspection | log, diff, blame |
| Advanced | stash, cherry-pick |
The best way to get comfortable with Git is to use it on real projects. Don't be afraid to experiment — Git almost always gives you a way back.