Git Commands Table
| Command | Action | When to use |
|---|---|---|
| git init | Create a new repository | Starting a project from scratch |
| git clone <url> | Copy an existing repository | Joining a project or deploying |
| git status | Show working tree state | Before every commit - always |
| git add . | Stage all changes | Before commit; use -p for selective staging |
| git commit -m "msg" | Save a snapshot | After staging; write clear messages |
| git log --oneline | Show commit history | Review what happened; --graph shows branches |
| git diff | Show unstaged changes | Review before staging; --staged for staged |
| git branch | List or create branches | git branch <name> creates; -d deletes |
| git checkout -b <name> | Create and switch to a branch | Starting new work; modern: git switch -c |
| git merge <branch> | Combine branches | After finishing feature work |
| git rebase <base> | Replay commits on a new base | Cleaning up before merge; --abort to escape |
| git push origin <branch> | Upload to remote | After committing; -u sets upstream on first push |
| git pull --rebase | Fetch and rebase onto remote | Sync with team before pushing |
| git reset --soft HEAD~1 | Undo last commit, keep changes | Wrong commit message or forgot a file |
| git revert <hash> | Undo a commit with a new commit | Safe undo on shared branches - never rewrite shared history |
| git stash | Temporarily save uncommitted work | Switch branches without committing; git stash pop to restore |
| git cherry-pick <hash> | Apply one commit to another branch | Hotfix or selectively moving changes |
Git has 150+ subcommands, but seventeen cover virtually all daily work: init, clone, status, add, commit, log, diff, branch, checkout, merge, rebase, push, pull, reset, revert, stash and cherry-pick. This table groups them by workflow and shows when to reach for each one.
The golden rules that prevent disasters: git status before every commit, git pull --rebase before every push, and git revert (not reset) on shared branches - because revert adds a new commit while reset rewrites history.
How to use
- Filter by command name or action - commit, branch, undo, remote - to find the one you need.
- Click any command to copy it into your terminal or documentation.
- Use the category chips to browse by workflow: commits, branching, undoing, remote.
Frequently asked questions
What is the difference between git reset and git revert?
git reset moves the branch pointer backward, rewriting history - dangerous on shared branches because everyone else’s commits diverge. git revert creates a new commit that undoes an old one - safe on shared branches because history is preserved. Rule: revert for anything already pushed, reset only for local mistakes.
What is the difference between git merge and git rebase?
Merge creates a merge commit with two parents, preserving the actual branch topology. Rebase replays your commits on top of the target branch, creating a linear history. Merge is safe for shared branches; rebase is for cleaning up local work before sharing. The golden rule: never rebase commits that others have already pulled.
What does git stash do?
git stash temporarily saves uncommitted changes and reverts the working tree to the last commit - letting you switch branches without committing half-finished work. git stash pop restores the changes. The stash is a stack: multiple stashes can be stacked with git stash, and retrieved with git stash pop --stash N.
What is the difference between git fetch and git pull?
git fetch downloads new commits from the remote but does not merge them - you can inspect them first with git log origin/main. git pull = fetch + merge (or rebase with --rebase). The safe pattern: fetch first, review with git log HEAD..origin/main, then merge or rebase deliberately.