Git 命令
初始设置
git init—Initialize new repo
git clone <url>—Clone remote repo
git config user.name—Set username
git config user.email—Set email
git config --list—View all config
git config --global—System-wide setting
基本操作
git add <file>—Stage file
git add .—Stage all changes
git add -p—Stage interactively
git commit -m "msg"—Commit staged changes
git commit --amend—Modify last commit
git reset HEAD <file>—Unstage file
git rm <file>—Remove & stage deletion
git mv <old> <new>—Rename & stage
分支
git branch—List branches
git branch <name>—Create branch
git checkout <branch>—Switch branch
git checkout -b <name>—Create & switch
git switch <branch>—Switch (modern)
git switch -c <name>—Create & switch (modern)
git branch -d <name>—Delete merged branch
git branch -D <name>—Force delete branch
git branch -m <new>—Rename current branch
git branch -a—List all (incl. remote)
远程仓库
git merge <branch>—Merge branch into current
git merge --no-ff—Merge with commit (no fast-forward)
git rebase <branch>—Rebase onto branch
git rebase --continue—Continue after conflict
git rebase --abort—Cancel rebase
git cherry-pick <hash>—Apply specific commit
git merge --squash—Squash merge into one commit
撤销操作
git remote -v—List remotes
git remote add origin <url>—Add remote
git push origin <branch>—Push to remote
git push -u origin <branch>—Push & set upstream
git pull—Fetch & merge
git fetch—Download remote changes
git push --tags—Push all tags
git pull --rebase—Rebase instead of merge
Inspect & Compare
git status—Working tree status
git log --oneline—Compact commit history
git log --graph—Visual branch graph
git diff—Unstaged changes
git diff --staged—Staged changes
git diff A..B—Compare two branches
git show <hash>—Show commit details
git blame <file>—Who changed each line
Undo & Reset
git checkout -- <file>—Discard file changes
git restore <file>—Discard changes (modern)
git reset --soft HEAD~1—Undo commit, keep staged
git reset --mixed HEAD~1—Undo commit, keep changes
git reset --hard HEAD~1—Undo commit, discard all
git revert <hash>—Create undo commit
git clean -fd—Remove untracked files
git reflog—History of HEAD changes
Stash
git stash—Save working changes
git stash pop—Apply & remove stash
git stash apply—Apply, keep stash
git stash list—List all stashes
git stash drop—Delete top stash
git stash show -p—Show stash diff
Tags
git tag v1.0—Lightweight tag
git tag -a v1.0 -m "msg"—Annotated tag
git tag—List all tags
git tag -d v1.0—Delete local tag
git push origin v1.0—Push tag to remote
git checkout v1.0—Checkout a tag
allprintabledoc.com