Git Cheatsheet

Git command reference organized by scenario for quick lookup.

Initialization & Config

git init

Initialize 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 --list

View all configuration

git config --global alias.co checkout

Set command alias

Basic Operations

git status

View working and staging area status

git add <file>

Add file to staging area

git add .

Add all changes to staging area

git add -p

Interactively select changes to stage

git commit -m "消息"

Commit staged changes

git commit -am "消息"

Add and commit tracked files

git commit --amend

Modify the most recent commit

git commit --amend --no-edit

Modify last commit without changing message

Branch Management

git branch

List local branches

git branch -a

List 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 -v

View remote repository info

git remote add origin <url>

Add remote repository

git remote set-url origin <url>

Change remote repository URL

git fetch

Fetch latest from remote (no merge)

git fetch --all

Fetch from all remotes

git pull

Pull remote changes and merge

git pull --rebase

Pull remote changes and rebase

git push

Push local commits to remote

git push -u origin <branch>

Push and set upstream branch

git push origin --delete <branch>

Delete remote branch

git push --force

Force push (use with caution)

View History

git log

View commit history

git log --oneline

Compact view of commit history

git log --graph --oneline

Graphical 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 diff

View working area vs staging area diff

git diff --staged

View 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 -fd

Remove untracked files and directories

git clean -fdn

Preview untracked files to be deleted

Stash

git stash

Stash current changes

git stash save "描述"

Stash with description

git stash list

List all stashes

git stash pop

Pop last stash and delete

git stash apply

Apply last stash without deleting

git stash apply stash@{n}

Apply specific stash

git stash drop stash@{n}

Drop specific stash

git stash clear

Delete all stashes

git stash branch <name>

Create branch from stash

Tag Management

git tag

List 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 --tags

Push 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 reflog

View all operations (for recovery)

git bisect start

Start 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 --init

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