A general-purpose baseline for now — the plan is to trim this down to the commands and flags I actually use day to day, and drop the rest.
Setup & config
| Command | Description |
|---|
git init | Create a new repository here. |
git clone <url> | Clone a remote repository. |
git config --global user.name "<name>" | Set your commit author name. |
git config --global user.email "<email>" | Set your commit author email. |
git config --list | Show effective config. |
Everyday snapshot
git status
Show the state of the working tree: staged, unstaged, and untracked changes.
| Flag | Description |
|---|
-s, --short | Compact one-line-per-file output. |
-b | Show branch and tracking info (used with -s). |
git add
Stage changes for the next commit.
| Flag | Description |
|---|
-A, --all | Stage everything (new, modified, deleted). |
-p, --patch | Interactively choose hunks to stage. |
-u | Stage modifications and deletions only, not new files. |
git commit
Record staged changes to the repository.
| Flag | Description |
|---|
-m "<msg>" | Commit message inline. |
-a | Auto-stage tracked, modified files before committing. |
--amend | Rewrite the previous commit instead of creating a new one. |
--no-verify | Skip pre-commit/commit-msg hooks. |
git diff
Show changes between commits, the working tree, and the index.
| Flag | Description |
|---|
--staged, --cached | Diff what’s staged against the last commit. |
<file> | Limit the diff to one file or path. |
<branch>..<branch> | Diff between two branches. |
Branching & merging
| Command | Description |
|---|
git branch | List local branches. |
git branch <name> | Create a new branch. |
git branch -d <name> | Delete a branch (safe — refuses if unmerged). |
git branch -D <name> | Force-delete a branch. |
git switch <branch> | Switch to an existing branch. |
git switch -c <branch> | Create and switch to a new branch. |
git checkout <branch> | Older equivalent of switch (also used for restoring files). |
git merge <branch> | Merge <branch> into the current branch. |
git rebase <branch> | Replay the current branch’s commits on top of <branch>. |
Inspecting history
git log
Show commit history.
| Flag | Description |
|---|
--oneline | One line per commit. |
--graph | Draw an ASCII branch/merge graph. |
-p | Show the full diff for each commit. |
-n <count> | Limit to the last <count> commits. |
--author="<name>" | Filter by author. |
git show
Show the details and diff of a single commit, tag, or object.
Working with remotes
| Command | Description |
|---|
git remote -v | List remotes and their URLs. |
git remote add <name> <url> | Add a new remote. |
git fetch <remote> | Download objects/refs without merging. |
git pull | Fetch and merge (or rebase, with --rebase) into the current branch. |
git push | Push the current branch to its upstream. |
git push -u <remote> <branch> | Push and set the upstream tracking branch. |
git push --force-with-lease | Force-push safely — fails if the remote has commits you haven’t seen. |
Undoing things
| Command | Description |
|---|
git restore <file> | Discard unstaged changes to a file. |
git restore --staged <file> | Unstage a file, keeping the changes. |
git reset --soft <commit> | Move HEAD, keep changes staged. |
git reset --mixed <commit> | Move HEAD, keep changes unstaged (default mode). |
git reset --hard <commit> | Move HEAD and discard all changes — destructive. |
git revert <commit> | Create a new commit that undoes <commit>. |
Stashing
| Command | Description |
|---|
git stash | Shelve uncommitted changes. |
git stash -u | Also stash untracked files. |
git stash list | List stashes. |
git stash pop | Reapply the most recent stash and drop it. |
git stash apply | Reapply the most recent stash but keep it in the list. |
| Command | Description |
|---|
git tag | List tags. |
git tag <name> | Create a lightweight tag. |
git tag -a <name> -m "<msg>" | Create an annotated tag. |
git push <remote> <tag> | Push a single tag. |
git push <remote> --tags | Push all tags. |