Development

Git Commands Cheat Sheet

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

Git version-control workflow with branches, commits and merge paths
Original concept artwork created for this Circuit Compass guide.

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

CommandPurpose
git initCreate a repository in the current directory.
git clone URLCopy a remote repository.
git statusShow branch and working-tree state.
git log --oneline --graphView compact history and branches.
git diffInspect unstaged changes.
git diff --stagedInspect changes prepared for commit.

Stage and commit changes

git add FILEStage a specific file.
git add -pInteractively stage selected hunks.
git commit -m "message"Create a commit from staged changes.
git restore --staged FILEUnstage a file without discarding its content.
git show COMMITInspect a commit and its patch.

Branches and remotes

git switch -c NAMECreate and switch to a branch.
git merge NAMEMerge a branch into the current branch.
git fetch --all --pruneUpdate remote references and remove stale ones.
git pull --ff-onlyUpdate only when a fast-forward is possible.
git push -u origin NAMEPush 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.

  1. Create a repository and commit notes.txt containing first line.
  2. Add second line, then run git add -- notes.txt.
  3. Add third line without staging it.
git status --short
git diff --staged -- notes.txt
git diff -- notes.txt

Status 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.txt

Unstaging 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.

Choose an undo operation by where the change lives

SituationAction to considerEffect
Wrong file staged; edits still wantedgit restore --staged -- FILEChanges the next commit without discarding the working file.
Published non-merge commit needs reversingInspect git show COMMIT; use git revert COMMIT on a clean working treeCreates a reversing commit rather than rewriting shared history. Review conflicts and the result.
Uncommitted content may be neededCopy it elsewhere or deliberately commit/stash before discardingUncommitted content may not be recoverable from history.

The revert reference explains continuation and abort options. A source-code revert does not undo an external deployment, database migration or sent message. Inspect those effects separately.

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.