Git Cheatsheet
Git command reference organized by scenario for quick lookup.
Initialization & Config
git initInitialize a new Git repository in the current directory
git clone <url>Clone remote repository to local
git clone <url> <dir>Clone remote repository to specified directory
git config --global user.name "名字"Set global username
git config --global user.email "邮箱"Set global email
git config --listView all configuration
git config --global alias.co checkoutSet command alias
Basic Operations
git statusView working and staging area status
git add <file>Add file to staging area
git add .Add all changes to staging area
git add -pInteractively select changes to stage
git commit -m "消息"Commit staged changes
git commit -am "消息"Add and commit tracked files
git commit --amendModify the most recent commit
git commit --amend --no-editModify last commit without changing message
Branch Management
git branchList local branches
git branch -aList all branches (including remote)
git branch <name>Create new branch
git branch -d <name>Delete merged branch
git branch -D <name>Force delete branch
git checkout <branch>Switch to specified branch
git checkout -b <name>Create and switch to new branch
git switch <branch>Switch branch (new command)
git switch -c <name>Create and switch to new branch (new command)
git merge <branch>Merge specified branch into current branch
git merge --no-ff <branch>Non-fast-forward merge (preserve history)
git rebase <branch>Rebase: rebase current branch onto target branch
git rebase -i <commit>Interactive rebase
Remote Operations
git remote -vView remote repository info
git remote add origin <url>Add remote repository
git remote set-url origin <url>Change remote repository URL
git fetchFetch latest from remote (no merge)
git fetch --allFetch from all remotes
git pullPull remote changes and merge
git pull --rebasePull remote changes and rebase
git pushPush local commits to remote
git push -u origin <branch>Push and set upstream branch
git push origin --delete <branch>Delete remote branch
git push --forceForce push (use with caution)
View History
git logView commit history
git log --onelineCompact view of commit history
git log --graph --onelineGraphical view of branch/merge history
git log -n <num>View last N commits
git log --author="名字"Filter commits by author
git log --since="2 weeks ago"Filter commits by time
git log -p <file>View commit history and diff for file
git blame <file>View last modification per line
git diffView working area vs staging area diff
git diff --stagedView staging area vs last commit diff
git diff <branch1> <branch2>View diff between two branches
git show <commit>View detailed info for a commit
Undo & Revert
git restore <file>Undo working area changes (new command)
git restore --staged <file>Unstage (new command)
git checkout -- <file>Undo working area changes (old command)
git reset HEAD <file>Unstage (old command)
git reset --soft <commit>Reset to commit, keep changes in staging area
git reset --mixed <commit>Reset to commit, keep changes in working area
git reset --hard <commit>Reset to commit, discard all changes
git revert <commit>Create new commit to revert specified commit
git revert -n <commit>Revert without auto-commit
git clean -fdRemove untracked files and directories
git clean -fdnPreview untracked files to be deleted
Stash
git stashStash current changes
git stash save "描述"Stash with description
git stash listList all stashes
git stash popPop last stash and delete
git stash applyApply last stash without deleting
git stash apply stash@{n}Apply specific stash
git stash drop stash@{n}Drop specific stash
git stash clearDelete all stashes
git stash branch <name>Create branch from stash
Tag Management
git tagList all tags
git tag <name>Create lightweight tag
git tag -a <name> -m "消息"Create annotated tag
git tag <name> <commit>Tag specific commit
git tag -d <name>Delete local tag
git push origin <tag>Push tag to remote
git push origin --tagsPush all tags to remote
git push origin --delete tag <name>Delete remote tag
Advanced Operations
git cherry-pick <commit>Apply specific commit to current branch
git cherry-pick <c1>..<c2>Apply multiple commits to current branch
git reflogView all operations (for recovery)
git bisect startStart bisect to locate bug
git bisect good <commit>Mark good commit
git bisect bad <commit>Mark bad commit
git worktree add <dir> <branch>Create worktree (multi-branch work)
git submodule add <url>Add submodule
git submodule update --initInitialize and update submodules
Usage Guide
Command Categories
This cheatsheet organizes Git commands into nine categories by scenario: Initialization & Config, Basic Operations, Branch Management, Remote Operations, History Viewing, Undo & Rollback, Stash & Storage, Tag Management, and Advanced Operations.
Search Function
The top search bar supports fuzzy matching by command text or description (based on Fuse.js). Entering keywords automatically filters categories containing matching commands; categories with no matches are hidden.
Copy Commands
!Hover over a command to reveal a copy button on the right. Click to copy the full command to clipboard; a toast notification appears on successful copy. Placeholders in angle brackets (e.g., <file>, <url>) need to be replaced with actual values.