Git Cheat Sheet: Essential Commands (Printable)
Keep this Git command cheat sheet nearby for everyday tasks: checking changes, creating commits, working with branches and remotes, and recovering safely. Commands are grouped by what you want to do, with short notes on when to use them.
Print this page: open your browser's print dialog with Ctrl + P on Windows or Linux, or ⌘ + P on Mac. Choose your preferred paper size and scale in the print dialog.
Start here: inspect your repository
| Command | What it does |
|---|---|
git status | Shows the current branch and working tree changes. |
git status --short | Shows a compact summary of changed files. |
git log --oneline --graph --decorate | Shows a compact commit history and branch references. |
git diff | Shows unstaged changes in tracked files. |
git diff --staged | Shows changes selected for the next commit. |
Stage and commit changes
git add <file> Stage one file
git add -p Choose changes interactively
git add . Stage changes under the current directory
git commit -m "Describe change" Create a commit
git commit --amend Replace the latest local commitReview git diff --staged before committing. Amending changes the commit ID; avoid amending commits already shared with others unless the team has agreed to rewrite that history.
Work with branches
git branch List local branches
git switch -c <name> Create and switch to a branch
git switch <name> Switch branches
git merge <branch> Merge a branch into the current branch
git branch -d <name> Delete a merged local branchSave or otherwise protect uncommitted work before switching branches. Git may prevent a switch if it would overwrite changes.
Connect to a remote
git remote -v List remote names and URLs
git fetch Download updates; do not integrate yet
git pull Fetch and integrate into the current branch
git push Send commits to the configured upstream
git push -u origin <branch> Push and set the upstream for this branchFetch before integrating when you want to inspect incoming changes. A push may be rejected if the remote has commits your local branch is missing; integrate them using your project's workflow instead of force-pushing blindly.
Undo or save work carefully
| Goal | Command | Note |
|---|---|---|
| Unstage a file | git restore --staged <file> | Keeps edits in the working tree. |
| Discard unstaged edits | git restore <file> | Replaces edits with the indexed version. |
| Undo a local commit, keep staged changes | git reset --soft HEAD~1 | Moves the current branch back one commit. |
| Reverse a shared commit | git revert <commit> | Creates a new commit that reverses it. |
| Save work temporarily | git stash push -u -m "message" | Includes untracked files; ignored files are excluded. |
Caution: git reset --hard and git clean can discard work. Check git status and confirm what will be removed before using destructive commands.
Configuration and ignore rules
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --list Show configuration
git check-ignore -v <file> Find the ignore rule matching a fileSet your author name and email with the identity you want recorded in commits. A .gitignore rule affects untracked files; it does not automatically untrack files already committed.
A safe everyday Git workflow
- Run
git statusto confirm your branch and changes. - Create or switch to the branch for your task.
- Make a focused change and review it with
git diff. - Stage the intended files and inspect
git diff --staged. - Commit with a message that explains the purpose of the change.
- Fetch or pull updates as needed, then push your branch.