Git Commands Cheat Sheet
A practical Git commands cheat sheet for setup, branches, commits, remotes, undoing changes, inspection and everyday collaboration.

This Git cheat sheet groups commands by job and explains what each one changes. Check git status and git diff before commands that rewrite or discard work.
Create, clone and inspect repositories
| Command | Purpose |
|---|---|
git init | Create a repository in the current directory. |
git clone URL | Copy a remote repository. |
git status | Show branch and working-tree state. |
git log --oneline --graph | View compact history and branches. |
git diff | Inspect unstaged changes. |
git diff --staged | Inspect changes prepared for commit. |
Stage and commit changes
git add FILE | Stage a specific file. |
git add -p | Interactively stage selected hunks. |
git commit -m "message" | Create a commit from staged changes. |
git restore --staged FILE | Unstage a file without discarding its content. |
git show COMMIT | Inspect a commit and its patch. |
Branches and remotes
git switch -c NAME | Create and switch to a branch. |
git merge NAME | Merge a branch into the current branch. |
git fetch --all --prune | Update remote references and remove stale ones. |
git pull --ff-only | Update only when a fast-forward is possible. |
git push -u origin NAME | Push a new branch and set its upstream. |
Undo safely
git restore FILE discards unstaged edits in a file, so inspect it first. git revert COMMIT creates a new commit that reverses an earlier one and is usually appropriate for shared history. git reset moves references and can discard work depending on its mode; do not copy a reset command without understanding the target and whether the commits are published.
Worked example: staged and unstaged changes
Git can hold two edits to a file: the version prepared for a commit and later changes in the working copy. Practice in a disposable local repository.
- Create a repository and commit
notes.txtcontainingfirst line. - Add
second line, then rungit add -- notes.txt. - Add
third linewithout staging it.
git status --short
git diff --staged -- notes.txt
git diff -- notes.txtStatus should show MM notes.txt: the first M describes the staged change and the second the unstaged change. The staged diff adds the second line; the ordinary diff adds the third. A commit now includes the second line, not the third.
git restore --staged -- notes.txt
git status --short
git diff -- notes.txtUnstaging leaves both lines in the working file. The ordinary diff now shows both relative to the committed version. This differs from git restore -- notes.txt, which can discard uncommitted content. See the restore reference for source and destination options.
The staged/unstaged and simple revert examples were checked in a disposable local repository on September 9, 2026 using git version 2.45.0.windows.1. They do not exercise merge conflicts or a remote deployment.
Frequently asked questions
What Git command should I run most often?
Run git status frequently. It shows the current branch and what is staged, modified or untracked.
How do I undo a pushed commit safely?
Use git revert to create a new reversing commit when other people may already have the history.
Sources and further reading
Use these official references for version-specific command behavior and options.
Found something that needs updating? Send a correction with the page URL and a supporting source.