Git Cheatsheet
Essential Git commands for daily work. Learn how to move fast, keep history clean, and recover safely.
When This Page Is Most Useful
This cheatsheet is most useful during everyday feature work, code review cleanup, and recovery after a mistaken reset, rebase, or merge. It focuses on commands developers actually reach for under pressure.
Create & Clone
| Key / Code | Description |
|---|---|
| git init | Initialize a new repository. |
| git clone <url> | Clone a remote repository. |
| git remote -v | Show current remotes. |
| git remote add origin <url> | Add a remote named origin. |
Branching
| Key / Code | Description |
|---|---|
| git branch | List local branches. |
| git branch -a | List local and remote branches. |
| git checkout -b <name> | Create and switch to a new branch. |
| git merge <branch> | Merge a branch into the current branch. |
Stage & Commit
| Key / Code | Description |
|---|---|
| git status | Show working tree status. |
| git add . | Stage all changes. |
| git add -p | Stage changes interactively. |
| git commit -m "msg" | Commit staged changes. |
Inspect History
| Key / Code | Description |
|---|---|
| git log --oneline --graph --decorate | Compact, visual history. |
| git diff | Show unstaged changes. |
| git diff --staged | Show staged changes. |
| git show <commit> | Show a specific commit. |
Undo Safely
| Key / Code | Description |
|---|---|
| git restore <file> | Discard unstaged changes in a file. |
| git reset HEAD <file> | Unstage a file. |
| git revert <commit> | Create a new commit that reverses a commit. |
| git reflog | Find lost commits after reset or rebase. |
Daily Flow
A clean daily workflow keeps history readable and reduces merge pain.
git checkout -b feature/login
# work...
git add -p
git commit -m "Add login form"
git pull --rebase origin main
# resolve conflicts if any
git push -u origin feature/loginCommon Git Mistakes
The most expensive Git mistakes come from using destructive commands without checking status, branch, and remote state first. Before reset, rebase, or force push, confirm what is staged, what branch you are on, and whether commits already exist on a shared remote.
Related
Go deeper into rebasing, history cleanup, and collaboration patterns after the basics.
Useful when cloning, pushing, or fixing authentication with remote repositories.
Schedule repository maintenance, backup jobs, or reporting tasks on servers.
Common when application code, infrastructure, and container configs live in the same repo.