Git Cheat Sheet
Essential Git commands: setup, staging, branching, merging, undo, and remote workflows.
Git records snapshots of a project and lets you move those snapshots between branches and remotes. Keep commits focused, inspect changes before committing, and choose undo commands according to whether history is already shared.
Setup and starting a repository
Configure identity once per machine or override it per repository. Set the default branch explicitly, then initialize a directory or clone an existing repository.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git init project
cd project
git clone https://example.com/team/project.git
| Command | Purpose |
|---|---|
git config --list | Show effective configuration. |
git config --show-origin --list | Show where each setting came from. |
git status | Summarize the worktree and staging area. |
git help <command> | Open documentation for a command. |
Stage and commit
The index is the proposed next snapshot. Review both unstaged and staged changes before creating a commit.
git status --short
git diff
git add src/app.ts README.md
git diff --cached
git commit -m "Add request validation"
| Command | Effect |
|---|---|
git add file | Stage a path. |
git add -p | Stage selected hunks interactively. |
git add -A | Stage additions, modifications, and deletions. |
git restore --staged file | Unstage without changing file contents. |
git commit --amend | Replace the latest local commit. |
git diff --stat | Show a compact change summary. |
Merge and rebase
Merging preserves branch topology with a merge commit when needed. Rebasing replays commits on a new base and produces a linear history; do not rebase commits others are already building on.
git switch main
git pull --ff-only
git merge --no-ff feature/search
git switch feature/search
git fetch origin
git rebase origin/main
# Resolve conflicts, then:
git add resolved-file
git rebase --continue
| Situation | Command |
|---|---|
| Abort a merge | git merge --abort |
| Abort a rebase | git rebase --abort |
| Continue after conflict | git add ... && git merge --continue |
| Preview merge result | git diff main...feature/search |
Undoing work safely
Use restore for files, reset for moving local branch pointers, and revert for a new inverse commit. The last option is safest for public history.
git restore path/to/file
git restore --source HEAD~1 -- path/to/file
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git revert <commit>
| Command | Changes commit history? | Typical use |
|---|---|---|
git restore file | No | Discard working-tree edits. |
git reset --soft HEAD~1 | Yes, keeps changes staged | Redo the last commit. |
git reset --hard HEAD~1 | Yes, discards tracked changes | Local-only recovery when certain. |
git revert <commit> | Adds an inverse commit | Undo a pushed commit. |
Remotes, inspection, and stash
Fetch downloads remote references without changing your branch. Pull combines fetch with merge or rebase according to configuration; push publishes local commits.
git remote -v
git remote add origin https://example.com/team/project.git
git fetch --prune origin
git pull --rebase origin main
git push -u origin feature/search
git log --oneline --decorate -10
git show --stat HEAD
git stash push -m "wip search UI"
git stash list
git stash pop