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.
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
Features, software and prices change. Confirm current details with these sources before making a decision.
Found something that needs updating? Send a correction with the page URL and a supporting source.