Skip to content

Git 命令速查

日常操作

bash
# 初始化 & 克隆
git init
git clone <url>
git clone <url> --depth=1   # 浅克隆,只拉最近一次提交

# 提交流程
git status
git add .
git add <file>
git commit -m "feat: 添加新功能"
git push origin main

# 撤销操作
git restore <file>          # 丢弃工作区修改
git reset HEAD <file>       # 取消暂存
git reset --soft HEAD~1     # 撤销上一次提交,保留修改
git reset --hard HEAD~1     # 撤销上一次提交,丢弃修改(危险)

分支管理

bash
git branch                  # 查看本地分支
git branch -a               # 查看所有分支(含远程)
git branch <name>           # 创建分支
git checkout -b <name>      # 创建并切换
git switch <name>           # 切换分支(新语法)
git merge <branch>          # 合并分支
git branch -d <name>        # 删除分支
git push origin --delete <name>  # 删除远程分支

查看历史

bash
git log --oneline --graph   # 图形化日志
git diff                    # 查看未暂存的修改
git diff --staged           # 查看已暂存的修改
git show <commit>           # 查看某次提交详情

常用场景

bash
# 暂存当前修改去处理紧急任务
git stash
git stash pop

# 从其他分支捡取某个提交
git cherry-pick <commit-hash>

# 修改最后一次提交信息
git commit --amend -m "新的提交信息"

# 查看某行代码是谁写的
git blame <file>

Commit 消息规范

类型含义
feat新功能
fix修复 Bug
docs文档修改
refactor代码重构
chore构建/工具变更

用 VitePress 构建