Skip to main content

Git Cheatsheet — 100+ Commands with Pitfalls and Real Examples

Git command cheat sheet — searchable, with explanations, common mistakes, and real examples.

  • Runs locally
  • Category Developer & DevOps
  • Best for Formatting, validating, shrinking, or inspecting code-adjacent text.
191 commands
Basic (25)
git init

Create a new empty git repository in the current directory.

Common pitfall: Running inside an existing repo creates a nested .git that confuses tooling. Run `git status` first.

Examples
git init
git init my-project
git clone <url>

Download a remote repository into a new local directory, with full history.

Common pitfall: For huge repos use --depth=1 (shallow) to skip history; you cannot push amends from a shallow clone without --unshallow.

Examples
git clone https://github.com/user/repo.git
git clone --depth=1 https://github.com/user/repo.git
git status

Show changed, staged and untracked files in the working tree.

Examples
git status
git status -s
git add <file>

Stage a file (or all files with .) for the next commit.

Common pitfall: `git add .` from a subdirectory only adds that subtree; `git add -A` stages everything in the repo including deletions.

Examples
git add src/index.ts
git add .
git add -A
git add -p
git commit -m "msg"

Record staged changes as a new commit with the given message.

Common pitfall: Forgetting to stage first creates an empty commit error; -am only stages tracked files (not new files).

Examples
git commit -m "fix: handle null user"
git commit -am "wip"
git commit --amend

Replace the last commit with a new one (message and/or content).

Common pitfall: Amending a pushed commit rewrites history — collaborators need to force-pull or face conflicts.

Examples
git commit --amend
git commit --amend --no-edit
git commit --amend -m "better message"
git push

Upload local commits on the current branch to the tracked remote.

Examples
git push
git push origin main
git push -u origin <branch>

Push a new local branch to the remote and set it as the upstream tracking branch.

Common pitfall: Without -u, future `git push` on this branch will keep asking for the remote/branch.

Examples
git push -u origin feature/login
git pull

Fetch changes from the tracked remote branch and merge them into the current branch.

Common pitfall: Default pull = fetch + merge, which creates merge commits. Many teams prefer `git pull --rebase`.

Examples
git pull
git pull --rebase
git fetch

Download new commits, refs and objects from a remote, but do NOT merge anything into your branches.

Examples
git fetch
git fetch origin
git fetch --all --prune
git clone --depth=1 <url>

Shallow clone with only the latest commit (no history). Much faster for CI builds.

Common pitfall: Cannot push or rebase from a shallow clone until you run `git fetch --unshallow`.

Examples
git clone --depth=1 https://github.com/torvalds/linux.git
git mv <src> <dst>

Rename or move a file and stage the change in one step.

Examples
git mv old-name.ts new-name.ts
git log -n 5

Show only the last 5 commits (any number works).

Examples
git log -n 5
git log -3 --oneline
git tag <name>

Create a lightweight tag on the current commit. Use -a for an annotated tag with message + tagger.

Common pitfall: Annotated tags (`git tag -a v1.0 -m "..."`) are recommended for releases — lightweight tags do not store metadata.

Examples
git tag v1.0.0
git tag -a v1.0.0 -m "first stable release"
git tag -d <name>

Delete a local tag. To delete the remote tag too: `git push --delete origin <name>`.

Examples
git tag -d v1.0.0
git push --delete origin v1.0.0
git rm <file>

Remove a file from the working tree AND stage the deletion.

Common pitfall: Use `git rm --cached <file>` to stop tracking without deleting from disk (e.g. for files newly added to .gitignore).

Examples
git rm secret.env
git rm --cached config.local.json
git commit

Commit staged changes, opening the configured editor to write the message.

Common pitfall: Leaving the editor with an empty message aborts the commit. Save a non-empty body to proceed.

Examples
git commit
git commit -v
git add -p

Interactively stage changes hunk by hunk, choosing which parts of a file to commit.

Common pitfall: A hunk too large to split shows up whole. Press `e` to hand-edit it, or `s` earlier to split.

Examples
git add -p
git add -p src/index.ts
git restore --source=<ref> <file>

Restore a file to its state at a specific commit, branch or tag.

Examples
git restore --source=HEAD~2 src/index.ts
git restore --source=main config.json
git rm -r --cached <dir>

Stop tracking a whole directory without deleting it from disk. Common after editing .gitignore.

Examples
git rm -r --cached node_modules
git rm -r --cached dist && git commit -m "chore: ignore dist"
git commit --allow-empty

Create a commit with no changes. Useful to trigger CI or mark a milestone.

Examples
git commit --allow-empty -m "ci: trigger rebuild"
git tag -l <pattern>

List tags, optionally filtered by a glob pattern.

Examples
git tag -l
git tag -l "v1.*"
git tag -l --sort=-version:refname
git show <tag>

Show an annotated tag, its message, tagger, and the commit it points to.

Examples
git show v1.0.0
git describe

Produce a human-readable name for the current commit based on the nearest tag (e.g. v1.0-14-g2414721).

Common pitfall: Fails with "no names found" if no annotated tag is reachable. Add `--tags` to include lightweight tags.

Examples
git describe
git describe --tags
git describe --tags --dirty
git archive

Export the tree of a commit as a tar or zip, without the .git directory.

Examples
git archive --format=zip --output=release.zip HEAD
git archive main | tar -x -C /tmp/snapshot
Branch (27)
git branch

List local branches; * marks the current branch.

Examples
git branch
git branch -a
git branch -vv
git branch <name>

Create a new branch from the current HEAD, but do not switch to it.

Examples
git branch feature/login
git branch -d <name>

Delete a local branch (safely — fails if it has unmerged commits).

Common pitfall: Use -D (capital) to force-delete unmerged branches — irreversible without reflog.

Examples
git branch -d feature/done
git branch -D feature/wip
git branch -m <new>

Rename the current branch.

Examples
git branch -m main
git branch -m old-name new-name
git checkout <branch>

Switch to an existing branch (legacy command — git switch is the modern replacement).

Common pitfall: `git checkout <file>` and `git checkout <branch>` are different commands sharing one name — confusing. Use `git switch` and `git restore`.

Examples
git checkout main
git checkout -
git checkout -b <new>

Create a new branch from current HEAD and switch to it in one step.

Examples
git checkout -b feature/login
git checkout -b hotfix origin/main
git switch <branch>

Switch to an existing branch. Modern replacement for `git checkout <branch>`.

Examples
git switch main
git switch -
git switch -c <new>

Create and switch to a new branch. Modern replacement for `git checkout -b`.

Examples
git switch -c feature/login
git switch -c hotfix origin/main
git merge <branch>

Merge the named branch into the current branch.

Common pitfall: Default merge creates a merge commit even when fast-forward is possible. Use `--ff-only` to refuse non-FF merges or `--no-ff` to always make a merge commit.

Examples
git merge feature/login
git merge --no-ff feature/login
git merge --ff-only origin/main
git merge --abort

Cancel an in-progress merge and restore the pre-merge state.

Examples
git merge --abort
git rebase <branch>

Replay commits from current branch on top of <branch>. Linear history, but rewrites commit IDs.

Common pitfall: Never rebase commits already pushed to a shared branch — collaborators will get conflicts on every pull.

Examples
git rebase main
git rebase origin/main
git rebase --abort

Cancel an in-progress rebase and restore the pre-rebase state.

Examples
git rebase --abort
git rebase --continue

After resolving conflicts during rebase, continue replaying remaining commits.

Examples
git add resolved-file.ts && git rebase --continue
git cherry-pick <sha>

Apply the changes from a specific commit onto the current branch as a new commit.

Common pitfall: The cherry-picked commit gets a NEW SHA. Cherry-picking the same change twice creates duplicate logic.

Examples
git cherry-pick a1b2c3d
git cherry-pick a1b2c3d..f4e5d6c
git cherry-pick -x a1b2c3d
git branch -a

List ALL branches (local + remote-tracking).

Examples
git branch -a
git branch --merged

List local branches fully merged into the current branch — safe to delete.

Examples
git branch --merged main
git branch --merged | grep -v "\*" | xargs git branch -d
git switch -

Switch back to the previously checked-out branch (like `cd -` for branches).

Examples
git switch -
git checkout -
git switch --detach <ref>

Check out a commit or tag in detached HEAD on purpose, e.g. to test an old release.

Examples
git switch --detach v1.0.0
git switch --detach HEAD~3
git branch -f <name> <ref>

Move an existing branch pointer to another commit without checking it out.

Common pitfall: Cannot force-move the branch you are currently on. Switch away first, or use `git reset` instead.

Examples
git branch -f staging main
git branch -f release HEAD~2
git branch --no-merged

List branches NOT yet merged into the current branch — the ones still holding unique work.

Examples
git branch --no-merged
git branch --no-merged main
git branch --show-current

Print just the name of the current branch (empty in detached HEAD). Script-friendly.

Examples
git branch --show-current
BR=$(git branch --show-current)
git merge --no-commit <branch>

Merge but stop before committing, so you can inspect or tweak the result first.

Examples
git merge --no-commit --no-ff feature/login
git rebase --onto <newbase> <upstream> <branch>

Replay only the commits in <upstream>..<branch> onto <newbase>. Surgically move a branch.

Common pitfall: Getting the three refs in the wrong order silently drops or duplicates commits. Verify with `git log --oneline` after.

Examples
git rebase --onto main feature-base feature/login
git rebase --skip

Skip the current commit during a rebase (e.g. when its changes already exist upstream).

Common pitfall: Skipping drops the commit entirely. Only skip when you are sure the change is already present.

Examples
git rebase --skip
git merge-base <a> <b>

Print the best common ancestor of two commits — the point where two branches diverged.

Examples
git merge-base main feature/login
git merge-base HEAD origin/main
git cherry-pick --continue

After resolving conflicts during a cherry-pick, finish applying the commit.

Examples
git add resolved.ts && git cherry-pick --continue
git cherry-pick --abort
git cherry-pick -n <sha>

Apply a commit's changes to the working tree and index but do NOT commit, so you can combine several.

Examples
git cherry-pick -n a1b2c3d
git cherry-pick --no-commit a1b2c3d f4e5d6c
Undo / recovery (24)
git reset HEAD <file>

Unstage a file (keep changes in the working tree).

Examples
git reset HEAD src/index.ts
git reset HEAD .
git reset --soft HEAD~1

Undo the last commit but keep changes staged. The most "non-destructive" reset.

Examples
git reset --soft HEAD~1
git reset --mixed HEAD~1

Undo the last commit and unstage everything, but keep working tree changes. This is the default.

Examples
git reset HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1

Throw away the last commit AND wipe matching working tree changes. Destructive.

Common pitfall: Anything not committed AND not in reflog is gone. Run `git stash` first if you might want it back.

Examples
git reset --hard HEAD~1
git reset --hard origin/main
git revert <sha>

Create a new commit that undoes the changes of <sha>. Safe on shared branches — does NOT rewrite history.

Examples
git revert a1b2c3d
git revert HEAD
git revert -m 1 <merge-sha>
git restore <file>

Discard unstaged changes in a file (modern replacement for `git checkout -- <file>`).

Examples
git restore src/broken.ts
git restore .
git restore --staged <file>

Unstage a file but keep working tree changes (modern replacement for `git reset HEAD <file>`).

Examples
git restore --staged src/index.ts
git stash

Save uncommitted changes (staged + unstaged) onto a stack and reset the working tree.

Common pitfall: By default `git stash` does NOT include untracked files; use `git stash -u` to include them.

Examples
git stash
git stash -u
git stash push -m "WIP login form"
git stash pop

Re-apply the most recent stash AND remove it from the stash stack.

Common pitfall: If pop has conflicts the stash stays on the stack — fix conflicts then `git stash drop`.

Examples
git stash pop
git stash pop stash@{2}
git stash list

List all stashes with their indexes and messages.

Examples
git stash list
git stash drop

Delete a single stash without applying it.

Examples
git stash drop
git stash drop stash@{1}
git reflog

Show local history of HEAD movements. Lifesaver for recovering lost commits / branches.

Common pitfall: Reflog is LOCAL ONLY — pushing/cloning does not copy it. Default expiry is 90 days.

Examples
git reflog
git reset --hard HEAD@{1}
git clean -fd

Delete untracked files (-f) and untracked directories (-d) from the working tree.

Common pitfall: Irreversible. Always `git clean -nfd` (dry-run) first to see what will be deleted.

Examples
git clean -nfd
git clean -fd
git clean -fdx
git restore --staged --worktree <file>

Discard a file's changes in BOTH the index and the working tree, resetting it to HEAD.

Common pitfall: This is destructive for unstaged edits — there is no working-tree reflog to undo it.

Examples
git restore --staged --worktree src/broken.ts
git stash apply

Re-apply a stash but KEEP it on the stack (unlike pop). Good for applying one stash to many branches.

Examples
git stash apply
git stash apply stash@{1}
git stash branch <name>

Create a new branch from the commit a stash was based on and apply the stash there. Best fix for "stash won't apply cleanly".

Examples
git stash branch wip-login stash@{0}
git stash show -p

Show the full diff of a stash before deciding to apply or drop it.

Examples
git stash show -p
git stash show -p stash@{1}
git stash clear

Delete ALL stashes at once. There is no confirmation.

Common pitfall: Cleared stashes are not in normal reflog views; recovery via `git fsck --unreachable` is painful. Be sure.

Examples
git stash clear
git reset --hard <sha>

Move the current branch to <sha> and make the working tree match it exactly, discarding everything after.

Common pitfall: Uncommitted work is destroyed. The branch's old position survives in `git reflog` for ~90 days.

Examples
git reset --hard a1b2c3d
git reset --hard origin/main
git revert --no-commit <sha>

Stage the inverse of a commit without committing, so you can revert several commits as one.

Examples
git revert --no-commit a1b2c3d
git revert -n HEAD~3..HEAD && git commit -m "revert last 3"
git revert --abort

Cancel an in-progress revert (e.g. a multi-commit revert that hit a conflict).

Examples
git revert --abort
git revert --continue
git clean -i

Interactively choose which untracked files to delete, instead of nuking them all with -f.

Examples
git clean -i
git clean -id
git checkout <sha> -- <file>

Pull a single file from an old commit into the working tree (and index) without touching anything else.

Examples
git checkout HEAD~3 -- src/config.ts
git checkout v1.0 -- README.md
git reset --merge

Reset like --hard but keep working-tree changes that differ from the index — safer after a bad merge.

Examples
git reset --merge
git reset --merge ORIG_HEAD
Remote (20)
git remote -v

List all remotes with their fetch and push URLs.

Examples
git remote -v
git remote add <name> <url>

Add a new remote under the given short name (usually `origin` or `upstream`).

Examples
git remote add origin https://github.com/me/repo.git
git remote add upstream https://github.com/orig/repo.git
git remote set-url <name> <url>

Change the URL of an existing remote (e.g. switching from HTTPS to SSH).

Examples
git remote set-url origin git@github.com:me/repo.git
git remote rename <old> <new>

Rename a remote.

Examples
git remote rename origin github
git remote remove <name>

Remove a remote entirely (does NOT delete the remote repo itself).

Examples
git remote remove old-upstream
git fetch --prune

Fetch from remote AND delete local remote-tracking branches whose remote counterparts are gone.

Examples
git fetch --prune
git fetch -p origin
git pull --rebase

Fetch from remote and rebase local commits on top — keeps history linear, avoids merge commits.

Common pitfall: If you have unrelated remote changes and rebase fails, run `git rebase --abort` to back out.

Examples
git pull --rebase
git config --global pull.rebase true
git push --tags

Push local tags to the remote (regular push does not include them).

Examples
git push --tags
git push origin v1.2.0
git push --delete origin <branch>

Delete a branch on the remote.

Examples
git push --delete origin feature/old
git push origin :feature/old
git ls-remote <url>

List refs (branches, tags) on a remote WITHOUT cloning. Handy for inspecting a repo before downloading.

Examples
git ls-remote https://github.com/torvalds/linux.git
git ls-remote --heads origin
git push origin HEAD

Push the current branch to a remote branch of the same name without typing the name twice.

Examples
git push origin HEAD
git push -u origin HEAD
git push --set-upstream origin HEAD

Push the current branch and set its upstream in one shot, even on first push of a new branch.

Examples
git push --set-upstream origin HEAD
git branch --set-upstream-to=<remote>/<branch>

Set or change which remote branch the current local branch tracks, without pushing.

Examples
git branch --set-upstream-to=origin/main
git branch -u origin/develop
git remote show <name>

Show detailed info about a remote: URL, tracked branches, and which are up to date or stale.

Examples
git remote show origin
git remote prune <name>

Delete local remote-tracking refs that no longer exist on the remote, without fetching new commits.

Examples
git remote prune origin
git remote prune origin --dry-run
git fetch <remote> <branch>

Fetch only a specific branch from a remote into FETCH_HEAD.

Examples
git fetch origin main
git fetch upstream release/2.0
git fetch <remote> <pr-ref>

Fetch a GitHub pull request branch directly by its refs/pull ref, then check it out.

Examples
git fetch origin pull/42/head:pr-42
git switch pr-42
git push --force-with-lease=<ref>:<expected>

Force-push but only if the remote ref still points at the exact SHA you expect. The strictest safe force.

Examples
git push --force-with-lease=main:a1b2c3d origin main
git push --all

Push all local branches to the remote in one command.

Common pitfall: This pushes even half-baked experimental branches. Prefer pushing branches explicitly.

Examples
git push --all origin
git remote get-url <name>

Print the URL of a remote (script-friendly alternative to parsing `git remote -v`).

Examples
git remote get-url origin
git remote get-url --push origin
Collaboration (19)
git push --force-with-lease

Force-push, but ONLY if the remote tip is what you last saw. The safe force-push.

Common pitfall: Plain --force overwrites teammates work silently. Always use --force-with-lease on shared branches.

Examples
git push --force-with-lease
git push --force-with-lease origin feature/login
git rebase -i HEAD~N

Interactively rewrite the last N commits: reorder, squash, fixup, reword, drop.

Common pitfall: Rebasing pushed commits = history rewrite. Only do it on your own branches before PR.

Examples
git rebase -i HEAD~5
git rebase -i main
git commit --fixup=<sha>

Create a fixup commit targeting <sha>. With `git rebase -i --autosquash` it auto-squashes into the right spot.

Examples
git commit --fixup=a1b2c3d
git rebase -i --autosquash main
git merge --squash <branch>

Squash all commits of <branch> into one staged change without committing — you then write one summary commit.

Examples
git merge --squash feature/login
git commit -m "feat: login flow"
git format-patch <branch>

Generate .patch files for each commit since <branch>. Used to mail patches to mailing lists (Linux kernel style).

Examples
git format-patch main
git format-patch -1 HEAD
git am <patch>

Apply a mailbox-style patch (created by format-patch) as one or more commits.

Examples
git am 0001-fix-bug.patch
git request-pull <start> <url>

Generate a summary of changes for asking an upstream maintainer to pull from you (pre-GitHub workflow).

Examples
git request-pull v1.0 https://github.com/me/repo feature-x
git pull --rebase --autostash

Auto-stash dirty working tree, pull --rebase, then unstash. Great alias for the daily pull.

Examples
git pull --rebase --autostash
git config --global rebase.autoStash true
git worktree add <path> <branch>

Check out an additional branch into a separate working directory — work on two branches without stashing.

Common pitfall: Worktrees share the same .git, so reflog / index ops still affect each other. List with `git worktree list`.

Examples
git worktree add ../hotfix hotfix/login
git worktree list
git worktree remove ../hotfix
git commit -s

Append a `Signed-off-by` trailer to the commit message — required by many open-source projects (DCO).

Examples
git commit -s -m "fix: null guard"
git commit --signoff --amend
git commit -S

GPG-sign the commit so its authorship can be cryptographically verified (the "Verified" badge).

Common pitfall: Needs `user.signingkey` and `gpg`/`ssh` set up first, or the commit fails with "gpg failed to sign".

Examples
git commit -S -m "release: v2.0"
git config --global commit.gpgsign true
git rebase -i --autosquash <base>

Interactive rebase that auto-orders fixup!/squash! commits next to their targets.

Examples
git rebase -i --autosquash main
git config --global rebase.autoSquash true
git commit --squash=<sha>

Like --fixup but keeps your message so you can edit the combined message during autosquash.

Examples
git commit --squash=a1b2c3d -m "more detail"
git apply <patch>

Apply a plain diff/patch to the working tree WITHOUT creating a commit (unlike `git am`).

Common pitfall: If the patch does not apply cleanly, add `--3way` to fall back to a 3-way merge using blob info.

Examples
git apply fix.patch
git apply --3way fix.patch
git apply --check fix.patch
git diff > <file>.patch

Save current unstaged changes as a patch file to share or apply elsewhere.

Examples
git diff > my-change.patch
git diff --staged > staged.patch
git range-diff <a>...<b>

Compare two versions of a patch series (e.g. before and after a rebase) commit by commit.

Examples
git range-diff main...feature@{1} main...feature
git range-diff origin/feature...feature
git worktree remove <path>

Remove a linked worktree once you are done with it, cleaning up the .git bookkeeping.

Common pitfall: Fails if the worktree has uncommitted changes. Commit/stash first, or add `--force` if you really want to discard.

Examples
git worktree remove ../hotfix
git worktree prune
git notes add -m "<text>"

Attach a note to a commit without changing its SHA — useful for review metadata or CI results.

Common pitfall: Notes are not pushed by default. Add a refspec or push `refs/notes/commits` explicitly to share them.

Examples
git notes add -m "reviewed by Alice" a1b2c3d
git push origin refs/notes/commits
git bundle create <file> <refs>

Pack commits into a single file you can move over USB/email when there is no network, then clone/fetch from it.

Examples
git bundle create repo.bundle --all
git clone repo.bundle restored-repo
Config & aliases (19)
git config --global user.name "Name"

Set your global git author name (applies to all repos).

Examples
git config --global user.name "Alice Wang"
git config --global user.email "you@example.com"

Set your global git author email.

Common pitfall: Use a per-repo `git config user.email` inside work repos to keep personal/work commit attribution separate.

Examples
git config --global user.email "alice@company.com"
git config user.email "alice@personal.com"
git config --list

Show every effective config setting (system + global + local), in evaluation order.

Examples
git config --list
git config --list --show-origin
git config --global alias.<name> <cmd>

Create a custom git alias. Common ones: co=checkout, br=branch, st=status, lg="log --oneline --graph".

Examples
git config --global alias.co checkout
git config --global alias.lg "log --oneline --graph --all"
git config --global core.editor "code --wait"

Set the editor git opens for commit messages, interactive rebase, etc.

Examples
git config --global core.editor "code --wait"
git config --global core.editor "vim"
git config --global init.defaultBranch main

Make `git init` create `main` as the default branch instead of `master`.

Examples
git config --global init.defaultBranch main
git config --global pull.rebase true

Make `git pull` always rebase by default (no more accidental merge commits).

Examples
git config --global pull.rebase true
git config --global core.autocrlf input

Convert CRLF to LF on commit but not on checkout. Recommended on macOS/Linux working with cross-platform repos.

Common pitfall: On Windows the recommended value is `true` (CRLF on checkout, LF on commit). Mixing them creates phantom diffs.

Examples
git config --global core.autocrlf input
git config --unset <key>

Remove a single git config entry.

Examples
git config --global --unset user.email
git config --unset alias.co
git config --global core.excludesFile <path>

Set a global ignore file applied to every repo (for editor/OS junk like .DS_Store).

Examples
git config --global core.excludesFile ~/.gitignore_global
git config --global rerere.enabled true

Turn on "reuse recorded resolution" so git replays your past conflict resolutions automatically.

Examples
git config --global rerere.enabled true
git config --global merge.conflictstyle zdiff3

Show the common ancestor in conflict markers, making it far easier to see what each side changed.

Examples
git config --global merge.conflictstyle zdiff3
git config --global fetch.prune true

Make every fetch automatically prune deleted remote-tracking branches.

Examples
git config --global fetch.prune true
git config --global push.autoSetupRemote true

Auto-set the upstream on first push, so `git push` works without `-u` on new branches.

Examples
git config --global push.autoSetupRemote true
git config --global rebase.autoStash true

Automatically stash and re-apply dirty changes around a rebase/pull --rebase.

Examples
git config --global rebase.autoStash true
git config --global diff.colorMoved zebra

Color moved lines differently from added/removed ones, so refactors read clearly in diffs.

Examples
git config --global diff.colorMoved zebra
git config --global url.<base>.insteadOf <other>

Rewrite remote URLs on the fly, e.g. force all GitHub HTTPS URLs to use SSH.

Examples
git config --global url."git@github.com:".insteadOf "https://github.com/"
git config --get-regexp <pattern>

Print config keys/values whose names match a regex (e.g. all aliases).

Examples
git config --get-regexp alias
git config --get-regexp "^remote\."
git config --global credential.helper <helper>

Choose how git caches/stores credentials (e.g. osxkeychain on macOS, cache with a timeout).

Common pitfall: The plain `store` helper writes the token in cleartext to ~/.git-credentials. Prefer the OS keychain.

Examples
git config --global credential.helper osxkeychain
git config --global credential.helper "cache --timeout=3600"
Inspect & history (31)
git log

Show commit history for the current branch.

Examples
git log
git log -n 10
git log --author="Alice"
git log --oneline --graph --all

Compact, one-line-per-commit graph of every branch — the most-used git log incantation.

Examples
git log --oneline --graph --all
git log --oneline --graph --all --decorate
git log -p

Show commit history WITH the full diff of each commit.

Examples
git log -p
git log -p src/index.ts
git log --stat

Show commit history with file-change statistics (which files, how many lines).

Examples
git log --stat
git log --stat -n 5
git diff

Show unstaged changes (working tree vs index).

Examples
git diff
git diff src/index.ts
git diff --staged

Show staged changes (index vs last commit) — what `git commit` would record.

Examples
git diff --staged
git diff --cached
git diff <branch1>..<branch2>

Show the diff between the tips of two branches.

Examples
git diff main..feature/login
git diff origin/main..HEAD
git diff <branch1>...<branch2>

Show changes on <branch2> since it diverged from <branch1> (uses merge base). Common for PR previews.

Common pitfall: Two dots vs three dots produce different diffs — three-dot is what PRs actually show.

Examples
git diff main...feature/login
git blame <file>

Show, for every line of a file, the commit and author that last changed it.

Common pitfall: Whitespace-only / formatting commits hide real authors. Use `git blame -w --ignore-rev <fmt-sha>` to skip them.

Examples
git blame src/index.ts
git blame -L 10,30 src/index.ts
git blame -w src/index.ts
git show <sha>

Show metadata and diff of a single commit.

Examples
git show a1b2c3d
git show HEAD
git show HEAD:src/index.ts
git shortlog -sn

Summarize commits per author with counts (sorted, numbered). Great for changelogs / credits.

Examples
git shortlog -sn
git shortlog -sn --since="1 month ago"
git bisect

Binary-search through commits to find the one that introduced a bug.

Examples
git bisect start
git bisect bad
git bisect good v1.0
git bisect reset
git log -S "<text>"

Find every commit that ADDED or REMOVED the given text — invaluable for "when did this string appear?".

Examples
git log -S "TODO" --source --all
git log -S "DEBUG_FLAG"
git grep <pattern>

Search tracked files for a regex pattern (faster and ignore-aware vs plain grep).

Examples
git grep "TODO"
git grep -n "TODO" -- "*.ts"
git diff --name-only

List ONLY the file names that changed, no diff body. Useful for piping to other commands.

Examples
git diff --name-only
git diff --name-only origin/main...HEAD | xargs eslint
git log --since/--until

Filter commits by date range. Accepts natural language like "2 weeks ago" or ISO dates.

Examples
git log --since="2 weeks ago"
git log --since=2026-01-01 --until=2026-02-01
git log --follow <file>

Show a file's history across renames, so you keep its story even after `git mv`.

Examples
git log --follow src/renamed.ts
git log --follow -p src/renamed.ts
git log <a>..<b>

List commits reachable from <b> but not from <a> — e.g. "what is on the branch but not on main".

Examples
git log main..feature/login
git log origin/main..HEAD
git log --first-parent

Follow only the first parent of merges, giving a clean mainline view without feature-branch noise.

Examples
git log --first-parent --oneline
git log --first-parent main
git log --merges

Show only merge commits — handy for reviewing what was integrated and when.

Examples
git log --merges --oneline
git log --no-merges
git log --grep=<pattern>

Search commit MESSAGES for a regex. Combine with -i for case-insensitive.

Examples
git log --grep="fix" -i
git log --grep="JIRA-123"
git log --pretty=format:"<fmt>"

Customize log output with placeholders like %h (short sha), %an (author), %ar (relative date), %s (subject).

Examples
git log --pretty=format:"%h %an %ar — %s"
git log --pretty=format:"%C(yellow)%h%Creset %s"
git show <ref>:<path>

Print the contents of a file as it existed at a given commit/branch, without checking it out.

Examples
git show HEAD~3:package.json
git show main:src/config.ts
git diff --word-diff

Show diffs word by word instead of line by line — great for prose, docs, and config files.

Examples
git diff --word-diff
git diff --word-diff=color README.md
git diff --check

Detect whitespace errors and leftover conflict markers before you commit.

Examples
git diff --check
git diff --staged --check
git blame -L <start>,<end> <file>

Blame only a line range of a file, instead of the whole thing.

Examples
git blame -L 40,80 src/index.ts
git blame -L :functionName src/index.ts
git rev-parse <ref>

Resolve a ref (branch/tag/HEAD~3) to its full 40-char SHA. The Swiss-army knife of scripting.

Examples
git rev-parse HEAD
git rev-parse --short HEAD
git rev-parse --abbrev-ref HEAD
git rev-list --count <ref>

Count commits reachable from a ref — e.g. how many commits are on a branch.

Examples
git rev-list --count HEAD
git rev-list --count main..feature
git cherry -v <upstream>

List your commits not yet in <upstream>; a + means unmerged, a - means already applied.

Examples
git cherry -v main
git cherry -v origin/main feature
git whatchanged

Like git log but always shows which files changed in each commit (older, file-focused log).

Examples
git whatchanged
git whatchanged --since="1 week ago"
git count-objects -vH

Report how much disk the repo objects use, in human-readable units. Spot bloat before `git gc`.

Examples
git count-objects -vH
Submodules (11)
git submodule add <url> <path>

Add another git repo as a submodule at the given path.

Common pitfall: Submodules pin a specific commit, not a branch. New clones need `git submodule update --init --recursive` or the dir will be empty.

Examples
git submodule add https://github.com/user/lib.git vendor/lib
git submodule init

Register submodules listed in .gitmodules into local .git/config.

Examples
git submodule init
git submodule update --init --recursive

Initialize, fetch and check out all submodules (and submodules-of-submodules) to their pinned commits.

Examples
git submodule update --init --recursive
git clone --recurse-submodules <url>
git submodule update --remote

Move each submodule to the latest commit of its tracked remote branch (then commit in the parent).

Examples
git submodule update --remote
git submodule update --remote vendor/lib
git submodule foreach <cmd>

Run a shell command inside every submodule. Useful for batch git operations.

Examples
git submodule foreach git pull
git submodule foreach "git checkout main && git pull"
git submodule deinit <path>

Remove a submodule cleanly: unregister it, then `git rm <path>` and commit.

Common pitfall: Just deleting the directory leaves stale .gitmodules / .git/config entries. Always deinit + rm.

Examples
git submodule deinit vendor/lib
git rm vendor/lib && rm -rf .git/modules/vendor/lib
git submodule status

Show each submodule's pinned commit and whether it is checked out, modified, or out of sync.

Examples
git submodule status
git submodule status --recursive
git submodule sync

Update each submodule's recorded URL in .git/config to match .gitmodules after the URL changed.

Examples
git submodule sync
git submodule sync --recursive
git submodule set-branch --branch <branch> <path>

Record which branch a submodule should track for `git submodule update --remote`.

Examples
git submodule set-branch --branch main vendor/lib
git clone --recurse-submodules <url>

Clone a repo and check out all its submodules in one step — no separate init/update needed.

Examples
git clone --recurse-submodules https://github.com/user/repo.git
git -c submodule.recurse=true pull

Pull the superproject AND update submodules to their newly pinned commits in one command.

Examples
git -c submodule.recurse=true pull
git config --global submodule.recurse true
Common mistakes (15)
git checkout -- <file> (LEGACY — destructive)

Discard ALL unstaged changes in <file>. Same as `git restore <file>`. Cannot be undone — there is no reflog for working-tree state.

Common pitfall: No prompt, no confirmation, no backup. Use `git stash` first if unsure.

Examples
git checkout -- broken.ts
git restore broken.ts
detached HEAD recovery

You checked out a commit/tag directly (e.g. `git checkout v1.0`). Any commits you make here will be lost unless you create a branch.

Common pitfall: If you accidentally left detached HEAD with new commits, find them in `git reflog` and `git branch save-me <sha>` immediately.

Examples
git switch -c new-branch
git reflog
git branch save-me HEAD@{0}
merge conflict resolution

When git stops mid-merge with conflicts: edit the <<<<<<< / ======= / >>>>>>> markers in each file, `git add` each resolved file, then `git commit` (or `git merge --continue`).

Common pitfall: Forgetting to `git add` the resolved file leaves the merge incomplete — commit will fail with "you have unmerged paths".

Examples
git status
git add resolved.ts
git commit
git merge --abort
recover lost commits with reflog

After a botched reset/rebase/branch-delete, `git reflog` lists every HEAD movement. Find the SHA you want and `git reset --hard <sha>` or `git branch rescue <sha>`.

Common pitfall: Reflog entries expire (default 90 days for reachable, 30 days for unreachable). Recover sooner rather than later.

Examples
git reflog
git branch rescue HEAD@{3}
git reset --hard HEAD@{5}
undo `git push --force`

If you force-pushed and overwrote teammates work: ask them to share their reflog (their local copy still has the old commits) and force-push that SHA back.

Common pitfall: GitHub keeps "lost" commits on its dangling-objects GC for ~14 days — you can sometimes recover via the API. ALWAYS use --force-with-lease.

Examples
# on a teammate's machine still holding the old SHAs:
git push --force-with-lease origin <sha>:main
remove a file from git history

A secret got committed. `git rm` only removes it going forward. To purge ALL history, use `git filter-repo` (modern, preferred) or BFG Repo Cleaner.

Common pitfall: After purge, rotate the secret anyway — anyone who cloned still has it. Then force-push the cleaned history.

Examples
git filter-repo --invert-paths --path secrets/key.pem
git push --force-with-lease origin --all
"fatal: refusing to merge unrelated histories"

You tried to merge/pull two repos that have no common ancestor (e.g. cloned and re-inited). Add `--allow-unrelated-histories` if you really want to merge them.

Examples
git pull origin main --allow-unrelated-histories
"Your branch and origin/main have diverged"

Both your branch and the remote gained commits. Decide: `git pull --rebase` to replay yours on top, or `git merge` to weave them. Never blindly `reset --hard`.

Common pitfall: A bare `git pull` here makes a noisy merge commit. Rebase keeps history linear if your commits are not yet shared.

Examples
git pull --rebase
git log --oneline --graph HEAD origin/main
"error: failed to push some refs"

The remote has commits you do not have locally. Fetch and integrate first (`git pull --rebase`), then push again. Do NOT reach for --force.

Common pitfall: Force-pushing here silently deletes the remote commits you never saw. Only force after you have integrated them.

Examples
git fetch origin
git rebase origin/main
git push
accidentally committed to main instead of a branch

Create a branch at the current commit to save the work, then reset main back so it matches the remote.

Common pitfall: Do the branch step FIRST. If you `reset --hard` main before branching, the commits are only in reflog.

Examples
git branch feature/oops
git reset --hard origin/main
git switch feature/oops
.gitignore not ignoring an already-tracked file

gitignore only affects untracked files. A file already committed keeps being tracked — untrack it with `git rm --cached`, then commit.

Examples
git rm --cached .env
echo ".env" >> .gitignore
git commit -m "chore: stop tracking .env"
wrong author on a commit

Fix the last commit with `git commit --amend --reset-author`; for older ones use interactive rebase with `--exec` or `git rebase -r`.

Common pitfall: Amending/rebasing rewrites SHAs. If already pushed to a shared branch, coordinate before force-pushing.

Examples
git commit --amend --reset-author --no-edit
git config user.email "right@example.com"
recover a deleted branch

A deleted branch's tip lives in the reflog. Find its SHA with `git reflog` and recreate the branch there.

Common pitfall: reflog is local-only. If the deletion happened on a machine you no longer have, the tip may be unrecoverable.

Examples
git reflog
git branch recovered a1b2c3d
git switch -c recovered HEAD@{5}
abort a half-finished interactive rebase

If an interactive rebase went sideways, `git rebase --abort` returns you to exactly where you started.

Common pitfall: Abort works only while the rebase is in progress. Once finished, undo via `git reset --hard ORIG_HEAD` instead.

Examples
git rebase --abort
git reset --hard ORIG_HEAD
CRLF / LF "whole file changed" diff

A diff showing every line changed is usually a line-ending mismatch. Normalize with a .gitattributes `* text=auto` rule and renormalize.

Examples
echo "* text=auto" >> .gitattributes
git add --renormalize .
git commit -m "chore: normalize line endings"

What this tool does

Searchable git command cheat sheet covering 100+ commands you actually type at the terminal. Every entry has the full syntax, a plain-English and Chinese explanation, the common pitfall people hit with it, and one or two real examples you can copy. Nine categories: basic (init, clone, status, add, commit, push, pull, fetch, tag, mv, rm), branch (branch, checkout, switch, merge, rebase, cherry-pick), undo & recovery (reset --soft / --mixed / --hard, revert, restore, stash, reflog, clean), remote (remote, fetch --prune, push -u, push --tags), collab (rebase -i, --autosquash, merge --squash, --force-with-lease, worktree), config & aliases, inspect & history (log, diff, blame, show, bisect, grep, log -S), submodules (add, init, update --remote, foreach, deinit), and common mistakes (detached HEAD recovery, merge conflict workflow, lost-commit recovery via reflog, undoing --force-push, purging secrets with filter-repo). Type any word and it filters live across commands, descriptions, pitfalls and examples; tap a category chip to scope. Bilingual EN/ZH, fully client-side, no tracking, no ads. Pair with our Regex Cheatsheet for the other "I always forget this syntax" reference, or Crontab Helper for the third thing every developer Googles weekly.

Tool details

Input
Text
The page exposes text boxes, numeric controls, file pickers, or structured inputs depending on the tool.
Output
Live result + Copy
The result area focuses on usable output, with copy, download, or preview actions when supported.
Privacy
Browser-side processing
The main tool logic does not call an external API, so inputs normally stay in the current tab.
Save / share
No account required
Open the page and use it; whether results survive refresh depends on the tool.
Performance budget
Initial JS <= 25 KB
No WASM budget is declared, keeping the tool quick to open on mobile.
Best fit
Developer & DevOps · Developer
Category and role tags drive related tools, internal links, and quick fit checks.

How to use

  1. 1. Input

    Paste or drop your content into the tool panel.

  2. 2. Process

    Click the button. All processing is local in your browser.

  3. 3. Copy / Download

    Copy the result or download to disk in one click.

How Git Cheatsheet fits into your work

Use it in the small gaps between coding, reviewing, debugging, and shipping.

Developer jobs

  • Formatting, validating, shrinking, or inspecting code-adjacent text.
  • Preparing snippets for documentation, tickets, commits, or handoff.
  • Checking a small payload quickly without switching tools.

Developer checks

  • Run irreversible transforms like minify or obfuscate on a copy.
  • Keep secrets out of pasted snippets unless the tool explicitly stays local.
  • Use your normal tests or linter before shipping transformed code.

Good next steps

These links move the current task into a more complete workflow.

  1. 1 .gitignore Generator Pick your stack — Node, Python, Go, Docker, macOS, VS Code — and get a deduped, sectioned .gitignore. Browser-only. Open
  2. 2 Regex Cheatsheet Interactive regex cheat sheet — quick reference for every flavor (JS, Python, PCRE). Open
  3. 3 Crontab Helper — Visual Builder & Explainer Visual crontab builder + human-readable explanation + next run preview. Open

Real-world use cases

  • Recovering a teammate's branch after a bad force-push

    A colleague force-pushed and wiped three of your commits from the shared feature branch. You filter the cheatsheet to "reflog", copy `git reflog`, find the SHA from before the overwrite, then run `git branch rescue HEAD@{6}` and force-push it back. The pitfall line reminds you reflog is local-only, so you do it on the machine where the commits last lived before garbage collection runs.

  • Onboarding a junior dev who has never touched git

    A new hire freezes at the terminal. You point them to the "Basic" category chip, which scopes the list to seven commands: init, clone, status, add, commit, push, pull. They work top to bottom in 20 minutes, and the pitfall on `git stash` ("does not include untracked files by default") saves them from losing a new file on day three.

  • Settling a checkout vs switch argument in code review

    A reviewer flags your `git checkout main` as outdated. You search "switch", read the entry explaining that Git 2.23 split checkout into `git switch` for branches and `git restore` for files, and copy the two-line rationale into the PR thread. The discussion ends in 30 seconds instead of a back-and-forth across three comments.

  • Purging a leaked API key from git history

    You committed a `.env` with a live token and pushed it. You search "secret", land on the `git filter-repo` entry, follow the example to strip the file from every commit, then force-push and rotate the key. The pitfall line warns that filter-repo rewrites SHAs, so you tell the two other contributors to re-clone instead of pulling.

Common pitfalls

  • Treating `git stash` as a safe backup. It skips untracked files unless you pass `-u`, so a brand-new file you never staged just gets left behind. Search "stash" and read the pitfall before you rely on it.

  • Typing `git push -f` out of habit. It silently erases a teammate's newer commit. Use `git push --force-with-lease`, which refuses unless the remote ref is exactly what you last saw.

  • Assuming `git checkout -- file` is reversible. Discarded working-tree changes were never committed, so reflog cannot bring them back. Stash or commit first, then discard.

Privacy

Everything runs in your browser. The command list is a static in-memory array, and your search text is matched locally with zero network requests, so no query, command, or filter choice ever leaves the tab. Nothing is written to the URL, so the page is safe to use behind a corporate proxy or fully offline.

FAQ

Tool combos

Folks in your role tend to reach for these alongside this tool.

Made by Toolora · 100% client-side · Updated 2026-07-02