Git 环境定制常用命令

面向:需要高效开发、排障、日志分析的工程师(C/C++ / SIP / 后端等)


1. 基础配置

复制代码
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

查看配置来源(排查冲突非常有用):

复制代码
git config --list --show-origin

2. Pager(分页器)优化

推荐配置(兼顾体验)

复制代码
git config --global core.pager "less -R -F -X"

说明:

  • -R:支持颜色输出

  • -F:一屏内容不分页

  • -X:退出不清屏(方便对比)


临时控制分页

复制代码
git --no-pager log        # 禁用分页
git -c core.pager=less log  # 强制分页

3. log 输出优化(高频)

图形化 + 简洁日志(强烈推荐)

复制代码
git config --global alias.lg "log --oneline --graph --decorate --all"

使用:

复制代码
git lg

可读性更高的日志格式

复制代码
git config --global alias.ll "log --pretty=format:'%C(yellow)%h%Creset %Cgreen%ad%Creset %Cblue%an%Creset %s' --date=short"

按文件追踪变更

复制代码
git log -p <file>

按关键字搜索提交

复制代码
git config --global alias.search "log -p -S"

git search "keyword"

4. 常用 alias(提升效率)

复制代码
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit

实用增强 alias

复制代码
git config --global alias.last "log -1 HEAD"
git config --global alias.blame "blame -w"

5. diff 优化

启用颜色

复制代码
git config --global color.ui auto

使用更友好的 diff(可选)

复制代码
git config --global core.pager "less -R"
git config --global interactive.diffFilter "diff-so-fancy --patch"

使用 vimdiff

复制代码
git config --global diff.tool vimdiff

6. pull / push 行为规范

避免误 push

复制代码
git config --global push.default simple

pull 默认 rebase(减少 merge commit)

复制代码
git config --global pull.rebase true

7. rebase / merge 优化

自动 squash fixup commit

复制代码
git config --global rebase.autosquash true

记住冲突解决(非常实用)

复制代码
git config --global rerere.enabled true

8. GPG 签名(GitLab 常见要求)

复制代码
git config --global commit.gpgsign true
git config --global user.signingkey <KEY_ID>

9. 大仓库性能优化

复制代码
git config --global core.preloadindex true
git config --global core.fscache true
git config --global gc.auto 256

10. 排障常用命令

查看配置来源

复制代码
git config --show-origin core.pager

查看环境变量

复制代码
echo $GIT_PAGER
echo $PAGER

临时覆盖配置

复制代码
git -c core.pager=less log

11. 推荐最小配置(直接复制)

复制代码
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

git config --global core.pager "less -R -F -X"
git config --global color.ui auto
git config --global push.default simple
git config --global pull.rebase true

git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.st status
git config --global alias.co checkout

12. 总结

核心优化点:

  • log:提升可读性(alias + format)

  • pager:提升阅读体验(less 参数)

  • 行为:统一 push / pull / rebase 策略

适用于日常开发、问题定位、代码审查等场景。


相关推荐
10mAh12 小时前
【Git】误删分支、reset --hard 后提交丢了怎么办?——reflog、fsck 与安全恢复实战
数据库·git·安全·回归
Lydro14 小时前
CentOS 7 安装高版本git
git·centos 7
guwentian1 天前
Git Worktree 实战:用并行多 Agent 把开发提速 N 倍
大数据·git·elasticsearch·wroktree
不吃鱼的羊1 天前
Git识别未修改的文件有修改
git
蓝黑墨水1 天前
一个git问题的处理
git
互联网中的一颗神经元1 天前
21 — 额外命令:同时开两个窗口、自动检查哨
git
_道隐_1 天前
git stash save 和 git stash push有什么区别?
git
_道隐_1 天前
git stash如何pop指定的stash change?
git
PC2005-cloud2 天前
Git学习笔记:GitHub Git Data API 完全指南,用 Go 操控 Git 底层对象
笔记·git·学习
Python私教2 天前
AI 并行编码的 Worktree 生命周期:创建、隔离与安全回收
人工智能·git