Git essentials
The Git commands you reach for weekly — grouped by what you're trying to do, with the undo table you'll want at 5 pm on a Friday.
Last updated
Everyday flow
| Command | What it does |
|---|---|
| git status | What's changed, what's staged, what branch you're on — the command between all other commands. |
| git add -p | Stage hunk by hunk, approving each — how clean, single-purpose commits get made. |
| git commit -m "msg" | Commit what's staged with a message. |
| git pull --rebase | Fetch and replay your local commits on top — linear history, no merge-bubble commits. |
| git push | Publish your commits to the remote branch. |
| git log --oneline -10 | The last ten commits, one line each. |
Branches
| Command | What it does |
|---|---|
| git switch -c feature-x | Create a branch and move to it in one step. |
| git switch - | Jump back to the previous branch — the cd - of Git. |
| git branch -d name | Delete a merged branch (-D to force an unmerged one). |
| git merge feature-x | Bring that branch's commits into this one. |
| git rebase main | Replay this branch onto main's tip — never on commits others already pulled. |
Undo (the Friday table)
| Command | What it does |
|---|---|
| git restore file | Throw away unstaged edits to a file — gone for good, so be sure. |
| git restore --staged file | Un-stage a file; the edits stay in your working tree. |
| git commit --amend | Redo the last commit — message or contents — if you haven't pushed it. |
| git revert abc123 | Undo a pushed commit safely, by adding a new commit that reverses it. |
| git reset --hard HEAD~1 | Erase the last local commit AND its changes — only for commits nobody else has. |
| git reflog | Every position HEAD has held — how "lost" commits get found again. |
Stash & inspect
| Command | What it does |
|---|---|
| git stash | Shelve uncommitted changes to get a clean tree; git stash pop brings them back. |
| git stash list | What's on the shelf. |
| git diff | Unstaged changes (--staged for what's about to be committed). |
| git blame -L 10,20 file | Who last touched lines 10–20, and in which commit. |
| git bisect start | Binary-search history for the commit that broke things — mark good/bad, Git halves the rest. |
Worth remembering
- →The distinction that keeps history safe: revert for anything pushed, reset only for commits still private to your machine.
- →If a command's output ever surprises you, git reflog is the safety net — commits are rarely lost, only unreferenced.
Questions
What is the difference between fetch and pull?
fetch downloads the remote's changes without touching your working tree; pull does that and immediately merges. Fetching first lets you look at what arrived before deciding how to integrate it.
How do I undo the last commit?
git reset --soft HEAD~1 keeps every change staged. The same command with --hard discards the work as well, which is the one-word difference worth being certain about before pressing enter.
What does reflog actually save?
Everywhere HEAD has been for the last few weeks, including commits no branch points at any more. It is why an accidental reset or a deleted branch is usually recoverable rather than gone.
Merge or rebase?
Merge preserves what actually happened; rebase produces a linear history that is easier to read. The rule that matters is not rebasing anything already pushed and shared, since it rewrites commits others hold.
Why does git say my branch has diverged?
Both you and the remote have commits the other lacks, usually after a rebase or an amend. Fetch and look at what is on each side before choosing between merging, rebasing, or force-pushing with lease.