← Back to DevKit

Git Command Cheat Sheet

GIT CHEAT SHEET · REFERENCE

Scenario-based Git command cheat sheet — undo, revert, branching, rebase, stash, submodules — searchable and copy-ready.

69 rows
SceneCOMMANDNotes
Configgit config --global user.name "Name"Set the global committer name
Configgit config --global user.email "you@example.com"Set the global committer email
Configgit config --global init.defaultBranch mainDefault the initial branch to main
Configgit config --global core.autocrlf inputCommit LF, leave checkout as-is (line endings)
Configgit config --global alias.st "status -sb"Create an alias (git st)
Configgit config --list --show-originList all config with its source file
Startgit initInitialize a repo in the current directory
Startgit clone <url> --depth 1Shallow clone (latest commit only, fast for big repos)
Startgit clone -b <branch> <url>Clone and check out a branch directly
Startgit status -sbShort status (branch + changes)
Startgit switch -c <new-branch>Create and switch to a new branch (modern)
Dailygit add -pStage hunk by hunk (split your commits)
Dailygit commit --amend --no-editFold changes into the last commit (before pushing)
Dailygit commit -m "feat: message"Commit with a conventional message
Dailygit restore <file>Discard working-tree changes (destructive)
Dailygit restore --staged <file>Unstage a file (keeps changes)
Dailygit diff --stagedDiff of staged-but-uncommitted changes
Dailygit clean -fdDelete untracked files and dirs (destructive)
Dailygit rm --cached <file>Untrack but keep locally (e.g. committed .env)
Branchgit branch -aList all local and remote branches
Branchgit branch -m old newRename a branch
Branchgit branch -d <branch>Delete a merged branch (-D to force)
Branchgit switch <branch>Switch branches (modern)
Branchgit worktree add ../hotfix hotfixWorktree: check out another branch in parallel
Branchgit cherry-pick <sha>Pick a single commit onto the current branch
Merge / Rebasegit merge --no-ff <branch>Merge with an explicit merge commit
Merge / Rebasegit merge --abortAbort the merge and restore the pre-merge state
Merge / Rebasegit rebase mainRebase the current branch onto main
Merge / Rebasegit rebase --continueContinue a rebase after resolving conflicts
Merge / Rebasegit rebase -i HEAD~3Interactive rebase over the last 3 commits
Merge / Rebasegit rebase --onto main <sha>Transplant a commit range onto another base
Undo / Revertgit commit --amendFix the last commit (message or content)
Undo / Revertgit reset --soft HEAD~1Undo the commit, keep changes staged
Undo / Revertgit reset --hard HEAD~1Undo the commit and discard changes (destructive)
Undo / Revertgit revert <sha>Safe rollback via an inverse commit (use on shared branches)
Undo / Revertgit reflogShow HEAD history (recover "lost" commits)
Undo / Revertgit reset --hard <sha>Hard-move the branch to a commit (destructive)
Undo / Revertgit checkout <sha> -- <file>Restore one file from a past commit
Remotegit remote -vList remote URLs
Remotegit remote set-url origin <url>Change the remote URL
Remotegit fetch --pruneFetch and prune stale remote branches
Remotegit push -u origin <branch>First push with upstream tracking
Remotegit push --force-with-leaseSafer force push (rejects if remote moved)
Remotegit push origin --delete <branch>Delete a remote branch
Remotegit pull --rebasePull with rebase (no merge bubbles)
Stashgit stash push -m "wip: msg"Stash changes with a message
Stashgit stash listList stashes
Stashgit stash popApply the latest stash and drop it
Stashgit stash apply stash@{2}Apply a specific stash, keep the entry
Stashgit stash -uStash including untracked files
Taggit tag -a v1.2.0 -m "release"Create an annotated tag (releases)
Taggit push origin v1.2.0Push one tag (push skips tags by default)
Taggit tag -d v1.2.0Delete a local tag
Taggit tag --sort=-creatordateList tags newest first
Submodulegit submodule add <url> <path>Add a submodule
Submodulegit submodule update --init --recursiveInit all submodules after cloning
Submodulegit submodule update --remoteUpdate submodules to their upstream latest
Submodulegit clone --recurse-submodules <url>Clone with submodules included
Historygit log --oneline --graph --all -20Graph overview of recent commits
Historygit log -p <file>Per-commit patch for one file
Historygit log --author="Name" --since="2 weeks ago"Filter history by author and time
Historygit blame -L 10,20 <file>Line-level blame for lines 10–20
Historygit show <sha>Show one commit in full
Historygit shortlog -snRank contributors by commit count
Troubleshootgit fsck --lost-foundCheck dangling objects (with reflog to recover)
Troubleshootgit diff main...featureThree-dot diff: what feature adds since the fork point
Troubleshootgit bisect startBisect to find the commit that broke it
Troubleshootgit update-index --assume-unchanged <file>Temporarily ignore local changes
Troubleshootgit ls-files | xargs rm && git checkout .Hard-restore the working tree to HEAD (last resort)
Filter by scene or keyword; commands marked destructive are irreversible. On shared branches prefer revert over reset --hard / force push (use --force-with-lease when you must).

>_ USAGE GUIDE

You use Git daily, but the low-frequency moves — "undo yesterday's commit", "transplant a range of commits onto another base", "recover a vanished commit" — send you searching every time. This cheat sheet is organized by scenario: config, start, daily commits, branching, merge/rebase, undo/revert, remote, stash, tag, submodule, history and troubleshooting, with keyword + scene filtering and one-click copy on every command.

  1. Browse by scene: pick a scene tab to focus, such as Undo / Revert when fixing history
  2. Search by keyword: reset, undo, force land straight on the relevant commands
  3. Copy and run: click the copy button per row and adjust the arguments in your terminal
  4. Respect the danger notes: commands marked destructive (reset --hard, clean -fd …) deserve a second look before Enter

>_ FAQ

Q1 · git reset or git revert?

revert creates an inverse commit that cancels a target commit — history only grows, and it is the right tool on shared branches. reset moves the branch pointer itself: fine for cleaning up unpushed local work, but reset plus force-push on a public branch scrambles everyone's history. Rule of thumb: reset rewrites your own history, revert fixes public history.

Q2 · How do I fix a wrong commit message?

Unpushed: git commit --amend rewrites the last commit (add --no-edit to keep the message). Pushed: amend then force-push — on shared branches prefer a documented revert plus a new commit instead. For older commits use interactive rebase (git rebase -i) and mark the line as reword.

Q3 · git push --force vs --force-with-lease?

--force overwrites the remote unconditionally. --force-with-lease first verifies the remote still sits where you last saw it and refuses otherwise — if someone pushed after you, the push fails. Use --force-with-lease essentially always; it only overwrites when nobody else has added commits.

Q4 · A commit disappeared — how do I get it back?

The branch pointer moved but the commit object still exists: git reflog lists HEAD's movements, find the sha, then git reset --hard <sha> or point a new branch at it. Reflog entries live about 90 days by default, and git fsck --lost-found recovers most accident scenes.

Q5 · A secret got committed — what now?

First git rm --cached <file> to untrack, and add it to .gitignore. The history still contains it: for private repos consider rewriting with git filter-repo followed by a force-push; for public repos or anything possibly leaked, rotate the secret immediately — cleaning history does not undo a leak.

Embed code

Paste the code below into your page to embed this tool as an iframe (nav and footer auto-hidden).

Heightpx
This tool runs entirely in your browser — input never leaves your device. Press Ctrl K to switch tools anytime.