Git Commands Cheat Sheet
These are the Git commands you'll use 95% of the time.
Getting Started
Clone a repository
git clone https://github.com/user/repo.git
Initialize new repo
git init
Check status
git status
Daily Workflow
See what changed
git diff
Stage specific files
git add file.js
Stage everything
git add .
Commit with message
git commit -m "Add login feature"
Push to remote
git push origin main
Branches
List branches
git branch
Create and switch to new branch
git checkout -b feature/login
Switch to existing branch
git checkout main
Delete branch (local)
git branch -d feature/login
Delete branch (remote)
git push origin --delete feature/login
Syncing
Get latest from remote (without merging)
git fetch
Get latest and merge
git pull
Pull with rebase (cleaner history)
git pull --rebase
Push new branch to remote
git push -u origin feature/login
Merging
Merge branch into current branch
git merge feature/login
Abort a merge with conflicts
git merge --abort
Rebase current branch onto main
git rebase main
Fixing Mistakes
Undo last commit (keep changes)
git reset --soft HEAD~1
Undo last commit (discard changes)
git reset --hard HEAD~1
Discard changes in a file
git checkout -- file.js
Amend last commit message
git commit --amend -m "New message"
Undo a pushed commit (creates new commit)
git revert abc123
Stashing
Save changes temporarily
git stash
Save with a name
git stash push -m "WIP: login form"
List stashes
git stash list
Apply most recent stash
git stash pop
Apply specific stash
git stash apply stash@{2}
Viewing History
View commit history
git log
Compact view
git log --oneline
See who changed what
git blame file.js
Search commits
git log --grep="bug fix"
Configuration
Set name and email
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Set default branch name
git config --global init.defaultBranch main
Set default editor
git config --global core.editor "code --wait"
Quick Reference Table
| Task | Command |
|------|---------|
| Stage all | git add . |
| Commit | git commit -m "msg" |
| Push | git push |
| Pull | git pull |
| New branch | git checkout -b name |
| Switch branch | git checkout name |
| Merge | git merge name |
| Undo commit | git reset --soft HEAD~1 |
| Stash | git stash |
| View log | git log --oneline |
Pro Tips
1. Commit often - Small commits are easier to review and revert
2. Write good messages - Future you will appreciate it
3. Pull before push - Avoid merge conflicts
4. Use branches - Never commit directly to main
5. Review before commit - git diff --staged shows what you're about to commit
Further Reading
For the complete reference, see the official Git documentation and the Pro Git book (free online).