最短路径:这篇是速查手册,不用从头读。
命令输出均在 git 2.56.0.windows.2 / 2026-10 实机验证。
一、第一次配置
身份
git config --global user.name "wulusai2333"
git config --global user.email "wulusai2333@github.com"这两个值会写进每一个提交,公开永久
- 用假名 + 非私人邮箱,不要填真实姓名
- 想让它关联到 GitHub 账号、显示头像,邮箱要用 GitHub 提供的那种
<用户名>@users.noreply.github.com - 邮箱填什么都行也意味着:填错了会在公开仓库里永久留痕(改历史很麻烦)
SSH key
2021 年 8 月 13 日起 GitHub 不再接受账号密码推送:
remote: Support for password authentication was removed on August 13, 2021.
remote: Please use a personal access token instead.所以要么用 SSH,要么用 token。SSH 一次配好长期省事:
ssh-keygen -t ed25519 -C "wulusai2333@users.noreply.github.com"为什么推荐 ed25519 而不是 rsa
ssh-keygen -t rsa 是很多老教程的写法,默认生成 3072 位。ed25519 更短、更快、同等安全,是现在的默认推荐。真要用 RSA,至少 -b 4096。
一路回车(密码短语可留空,留空则每次用 key 不再问密码)。然后看公钥:
cd ~/.ssh && ls # 应该能看到 id_ed25519 和 id_ed25519.pub
cat id_ed25519.pub # 复制输出的整行把整行(含开头的 ssh-ed25519)粘到 GitHub:Settings → SSH and GPG keys → New SSH key。
验证配置
ssh -T git@github.com成功时输出:
Warning: Permanently added the RSA host key for IP address 'x.x.x.x' to the list of known hosts.
Hi wulusai2333! You've successfully authenticated, but GitHub does not provide shell access.看到 Hi <你的用户名>! 就是成功了。第一行的 IP 是 GitHub 的 SSH 入口地址,正常现象。
仓库地址从 HTTPS 换成 SSH
git remote set-url origin git@github.com:<用户名>/<仓库>.git注意语序是 remote set-url origin,不是 remote origin set-url(这是常见笔误)。
二、日常命令速查
# —— 拿到仓库 ——
git clone <地址> # 克隆
git clone -b <分支> <地址> # 只拉某个分支
git clone --depth 1 <地址> # 浅克隆,只要最新一次提交(大仓库提速明显)
# —— 看状态 ——
git status # 完整状态
git status --short # 精简(脚本里更常用)
git diff # 工作区 vs 暂存区
git diff --staged # 暂存区 vs 最后一次提交(即将提交什么)
git diff HEAD # 工作区 + 暂存区 vs 最后一次提交
# —— 提交 ——
git add <文件> # 暂存单个文件
git add -A # 暂存所有改动(含删除)
git commit -m "说明" # 提交
git commit -am "说明" # 已跟踪文件的改动,跳过 add(新文件不含)
git commit --amend # 修补最后一次提交(换 SHA,别对已推送的用)
# —— 远程 ——
git remote -v # 看远程地址
git remote add origin <地址> # 关联远程
git remote set-url origin <新地址> # 改地址
git fetch --prune # 只更新远端状态,不动工作区;顺手清理已删的远程分支
git pull --rebase # 拉取并把本地提交挪到后面(历史保持直线)
git push -u origin <分支> # 首次推送并建立跟踪关系
git push # 之后直接推
# —— 分支 ——
git switch -c <分支> # 新建并切换
git switch <分支> # 切换
git branch -a # 列出所有分支(含远程)git commit -am 不会带上新文件
-a 只处理已被跟踪的文件的修改和删除。新建的文件必须先 git add。这是"我明明提交了但文件不在"的最常见原因。
三、别名:把高频命令缩短
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.st "status --short"
git config --global alias.last "log -1 --stat"之后:
$ git config --get alias.lg
log --oneline --graph --decorate --all自己常用的几条就够,别抄一长串记不住的。
别名里避免 ! 开头的 shell 别名
alias.x "!f() { ... }" 会在 shell 里执行,跨平台(Git Bash / PowerShell / WSL)行为不一致,还容易被当成注入点。纯 Git 参数的别名最稳。
四、只提交一部分改动
同一个文件改了三处,但只想提交其中一处 —— 用交互式暂存:
$ git add -p
diff --git a/h.txt b/h.txt
@@ -1,3 +1,3 @@
-line1
+line1 CHANGED
line2
line3
(1/1) Stage this hunk [y,n,q,a,d,e,p,P,?]?按键含义:
| 键 | 作用 |
|---|---|
y | 暂存这一块 |
n | 跳过这一块 |
s | 把大块再拆小(文件改动密集时用) |
e | 手动编辑要暂存的内容 |
q | 退出(已暂存的保留) |
? | 看完整帮助 |
不习惯交互式的话,等价做法是分文件提交:
$ git diff --stat
h.txt | 2 +-
i.txt | 1 +
$ git add i.txt
$ git diff --staged --stat
i.txt | 1 +五、查历史
$ git log --oneline
78f3ee9 feat: 加第四行
b06b9d4 fix: 改第二行
5ebc777 feat: 加入三行
$ git log -p -1 # 看最后一次提交的完整 diff
$ git blame -L 2,2 log.txt # 第 2 行是谁、哪个提交改的
b06b9d4 (wulusai2333 2026-10-20 04:05:09 +0800 2) two changed
$ git log -S "four" --oneline # 哪个提交 增删了 包含 "four" 的内容(找 bug 引入点神器)
78f3ee9 feat: 加第四行
$ git log --oneline --since="10 minutes ago"
$ git log --oneline --author="wulusai2333"
$ git log --oneline -- <路径> # 只看某个目录/文件的历史| 需求 | 命令 |
|---|---|
| 一行一条看历史 | git log --oneline |
| 看图形化分支关系 | git log --oneline --graph --all |
| 看某次提交改了什么 | git show <SHA> |
| 看某次提交的文件清单 | git show --stat <SHA> |
| 某行是谁写的 | git blame <文件> |
| 哪个提交引入/删除了某个字符串 | git log -S "字符串" |
| 某个提交在哪些分支上 | git branch --contains <SHA> |
| 两个分支的差异 | git diff master..feature --stat |
| 只属于 feature 的提交 | git log --oneline master..feature |
六、换行符:Windows 上的经典坑
warning: in the working copy of 'a.txt', LF will be replaced by CRLF the next time Git touches it原因:Windows 用 CRLF 换行,Linux/macOS 用 LF。Git 想在两边都让你看着舒服,于是有个自动转换设置 core.autocrlf:
| 值 | 行为 | 适合 |
|---|---|---|
true | 提交时 CRLF→LF,检出时 LF→CRLF | Windows 单机开发(Git for Windows 的默认值) |
input | 提交时 CRLF→LF,检出时不动 | Linux/macOS |
false | 完全不转换 | 团队里有混合系统、或仓库已固定换行符 |
三条处理路线,从推荐到不推荐:
# 1【推荐】在仓库里放 .gitattributes,让换行符由仓库说了算,与每个人的设置无关
printf '* text=auto eol=lf\n' > .gitattributes
git add .gitattributes
# 2 对已经混乱的文件做一次规范化
git add --renormalize .
# 3【不推荐】简单粗暴地全局关掉转换
git config --global core.autocrlf false查看某个文件在仓库里到底存的什么:
$ git ls-files --eol a.txt
i/crlf w/crlf attr/ a.txti/ 是 index(仓库里存的),w/ 是工作区。两边不一致就说明转换在起作用。
警告本身通常无害,但别忽略"整文件 diff"
如果一次改动没改几行,diff 却显示整个文件都变了,几乎一定是换行符被整体转换了。这种提交会污染 git blame 和 review,必须先停下来处理。
七、自签名证书(内网 GitLab 常见)
SSL certificate problem: self signed certificate正确做法是把证书装上,而不是关掉校验:
# 临时用一次
GIT_SSL_CAINFO=/etc/ssl/certs/your_root_cert.pem git clone https://your-gitlab.example.com/group/repo.git
# 让这个仓库长期生效
git config http.sslCAInfo /etc/ssl/certs/your_root_cert.pemWindows 上导入到系统受信任根证书(这样 Git for Windows 和浏览器都认):
- 从服务端拿到
.crt/.cer证书文件(浏览器访问站点 → 地址栏锁图标 → 导出证书) Win + R输入certmgr.msc打开"管理用户证书"- 展开 受信任的根证书颁发机构 → 证书,右键 → 所有任务 → 导入
- 按向导选择刚导出的证书,一路下一步完成
# 实在没法装证书时的临时方案(危险,会失去中间人攻击防护)
git -c http.sslVerify=false clone https://your-gitlab.example.com/group/repo.git别把 http.sslVerify false 写成 global
git config --global http.sslVerify false 会让你所有仓库的 HTTPS 校验失效,包括公网的。要临时用也只在单条命令上加 -c。
八、统计代码量
按时间段统计每个人的增删行数(--author 换成具体人名即是个人统计):
git log --format='%aN' | sort -u | while read name; do
echo -en "$name\t"
git log --author="$name" --pretty=tformat: --numstat \
--since=2026-01-01 --until=2026-12-31 \
| awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added: %s, removed: %s, net: %s\n", add, subs, loc }' -
done别用代码行数考核
--numstat 统计的是diff 行数,包含空行、注释、生成的代码、格式化产生的整文件变更。换行符问题(上一节)能让一次提交凭空多出几千行。这个数字只能用来观察趋势。
九、.gitignore
# 忽略所有 .a 文件
*.a
# 但 lib.a 要跟踪,上面的规则对它无效
!lib.a
# 只忽略当前目录的 TODO,不含子目录里的
/TODO
# 忽略整个 build 目录
build/
# 忽略 doc/notes.txt,但 doc/server/arch.txt 不受影响
doc/*.txt
# 忽略 doc 下任意层级的 pdf
doc/**/*.pdf已经提交过、之后才加进 .gitignore 的文件不会自动失效,要手动移出跟踪:
git rm --cached <文件> # 只从 Git 移除,本地文件保留
git rm -r --cached <目录>十、冷门但致命:bisect
问题:功能坏了,但最近几十个提交里不知道哪个引入的。手工一个个试太慢,让 Git 二分:
git bisect start
git bisect bad # 当前(坏的)
git bisect good <已知好的提交> # 找到一个确定没问题的历史提交
# Git 会自动切到中间那个提交,你测一下,然后:
git bisect good # 或者 git bisect bad
# 重复几次后,Git 报出第一个坏提交
git bisect reset # 收工,回到原来的分支如果"测一下"能写成命令,直接自动化(退出码 0 = 好,非 0 = 坏):
$ git bisect start HEAD cb313be
$ git bisect run sh -c "! grep -q BROKEN s.txt"
...
a0fbe504f789ae1ff408d09d96dd0922f5f35265 is the first 'bad' commit
step 4
bisect found first 'bad' commit用 bisect run 时注意退出码方向
约定是退出码 0 表示"好"。grep 找到关键字时退出码是 0(表示"好"),但我们要找的是"文件里有 BROKEN 就是坏",所以加了 ! 把结果反转。写反了会得到完全相反的结论。
附:上面这些命令里最容易写错的几条
| 常见错误写法 | 正确写法 | 说明 |
|---|---|---|
git remote origin set-url <url> | git remote set-url origin <url> | 语序 |
git branch -b <名> origin/<名> | git switch -c <名> origin/<名> | -b 是 checkout 的参数 |
git push origin –d <分支> | git push origin -d <分支> | 中文输入法打出的长横线 – 不是减号 |
git push -f origin master | git push --force-with-lease origin master | 避免覆盖别人的提交 |
git checkout -- <文件> | git restore <文件> | 新命令语义更清晰 |
git reset --hard 撤提交 | 先用 git stash 或 git reset --soft | --hard 会真的删掉未提交改动 |
参考
- Git 官方文档
- Pro Git 中文版(免费、权威,遇到不懂的概念查这本)
- gitignore 模板合集
