0%

从 clone 到 cherry-pick:Git 进阶速查表

1. 环境一次搞定

1
2
3
4
git config --global user.name  "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.autocrlf input

2. 日常三件套 & 常用组合

目的 命令 备注
克隆仓库 git clone <url> 默认目录名与仓库同名
工作区状态 git status -sb -s 简洁 -b 显示分支
添加改动 git add .git add -p -p 交互式块级添加
提交 git commit -m "feat: xxx" 首行 ≤ 50 字符
首次推送 git push -u origin main -u 建立追踪
拉取更新 git pull --rebase origin main 保持线性历史
查看日志 git log --oneline --graph --decorate --all 拓扑图

3. 分支与合并

任务 命令 说明
新建并切换 git switch -c feature/login Git ≥2.23 推荐
本地分支列表 git branch -vv 显示追踪关系
删除已合并分支 git branch -d feature/login 强制删除加 -D
合并分支 git merge --no-ff feature/login 保留分支信息
变基整理 git rebase -i HEAD~3 压缩/重排提交
挑拣提交 git cherry-pick <hash> 把指定 commit 拿过来

4. 撤销与回退

场景 命令 副作用
撤销工作区修改 git restore <file>
撤销已 add 文件 git restore --staged <file>
修改最后一次提交 git commit --amend 改写历史
回退提交但保留改动 git reset --soft HEAD~1 改动回到暂存区
彻底回退 git reset --hard HEAD~1 工作区也回退,危险
安全反做 git revert <hash> 生成一次反向提交

5. 远程协作

任务 命令
查看远程 git remote -v
添加 upstream git remote add upstream <url>
同步上游 git fetch upstream && git rebase upstream/main
推送新分支 git push -u origin feature/login
删除远程分支 git push origin --delete feature/login

6. 储藏、标签、子模块

任务 命令
临时保存 git stash push -m "WIP"
弹出储藏 git stash pop
打轻量标签 git tag v1.0.0
打附注标签 git tag -a v1.0.0 -m "release"
推送标签 git push origin --tags
初始化子模块 git submodule update --init --recursive

7. 调试与搜索

任务 命令
二分定位 bug git bisect start
git bisect bad HEAD
git bisect good <tag>
git bisect run ./test.sh
查找谁改了某行 git blame -L 50,60 <file>
搜索历史内容 git log -S "keyword" --patch
查看任意文件某版本 git show <hash>:<file>
  • 本文作者: Kimariyb
  • 本文链接: https://ikuns.icu/025/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!