03-我的git配置
配置脚本
#!/bin/bash
# Git 配置脚本
cp -r .git-hooks ~/.config/git/.git-hooks
git config --system include.path '~/.config/git/.gitconfig'
git config --global core.hooksPath ~/.config/git/.git-hooks
# 1. 基本配置
# 设置用户信息
git config --global user.name "yanggenjie" # 替换为你的名字
git config --global user.email "yang_genjie@126.com" # 替换为你的邮箱
# 设置默认编辑器
git config --global core.editor "nvim" # 设置默认编辑器为 Visual Studio Code: code --wait
# 设置默认分支名称
git config --global init.defaultBranch main # 默认分支名称为 main
# 2. 提交相关配置
# 设置提交模板
git config --global commit.template ~/.config/git/template # 替换为你的提交模板路径
# 启用 GPG 签名
# git config --global commit.gpgsign true # 强制对所有提交进行 GPG 签名
# git config --global user.signingkey YOUR_GPG_KEY_ID # 替换为你的 GPG 密钥 ID
# 3. 性能优化
# 启用 Delta 存储
git config --global core.compression 9 # 设置最高级别的压缩
# 启用多线程
git config --global pack.threads 4 # 根据你的 CPU 核心数调整
# 启用浅克隆
# git config --global clone.depth 1 # 默认使用浅克隆,减少克隆时间
# 4. 安全性增强
# 启用分支保护
# git config --global branch.master.rebase true # 切换到主分支时自动变基
# 启用自动清理
git config --global gc.auto 250 # 当未合并的分支数量达到 250 时自动清理
git config --global gc.autoDetach true # 在后台自动运行垃圾回收
# 5. 工作流优化
# 启用分支清理
# git config --global branch.autosetuprebase always # 创建分支时自动设置变基
# 忽略文件模式
git config --global core.fileMode false # 忽略文件权限的更改
# 忽略空格差异
git config --global core.whitespace "fix,-indent-with-non-tab,trailing-space,cr-at-eol" # 自动修复空格问题
# 显示未跟踪文件
git config --global status.showUntrackedFiles all # 在状态显示中包含所有未跟踪的文件
# 6. 美化输出
# 启用颜色
git config --global color.ui auto # 在终端中显示颜色
# 设置美化日志别名, 美化日志输出
git config --global alias.lg "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ad)%C(reset) %C(white)%s%C(reset) %C(dim cyan)- %an%C(reset)%C(bold yellow)%d%C(reset)' --date=format:'%Y-%m-%d %H:%M:%S %a'"
git config --global alias.lg1 "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%ad%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset) %C(dim white)- %an%C(reset) %n %C(white)%s%C(reset) %C(white)%b%C(reset) ' --date=format:'%Y-%m-%d %H:%M:%S %a' --all"
git config --global alias.lga "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ad)%C(reset) %C(white)%s%C(reset) %C(dim cyan)- %an%C(reset)%C(bold yellow)%d%C(reset)' --date=format:'%Y-%m-%d %H:%M:%S %a' --all"
git config --global alias.lga1 "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%ad%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset) %C(dim white)- %an%C(reset) %n %C(white)%s%C(reset) %C(white)%b%C(reset) ' --date=format:'%Y-%m-%d %H:%M:%S %a' --all"
git config --global alias.graph "log --graph --oneline --all"
# 7. 别名配置
# 简化命令别名
git config --global alias.st "status" # 简化 `git status` 为 `git st`
git config --global alias.a "add" # 简化 `git add` 为 `git a`
git config --global alias.c "commit" # 简化 `git commit` 为 `git c`
git config --global alias.l "log" # 简化 `git log` 为 `git l`
git config --global alias.pu "pull" # 简化 `git pull` 为 `git pu`
git config --global alias.ps "push" # 简化 `git push` 为 `git ps`
git config --global alias.br "branch" # 简化 `git branch` 为 `git br`
git config --global alias.dbr "branch -d" # 简化 `git branch -d` 为 `git dbr`
# 8. 其他实用配置
# 忽略文件名大小写
git config --global core.ignorecase false # 在 Windows 或 macOS 系统中,避免因文件名大小写问题导致的冲突
# 设置 Git 推送配置
# git config --global push.autoSetupRemote true # 自动设置远程分支名称与本地分支一致
# git config --global push.default current # 默认推送当前分支
git config --global push.followTags true # 推送时包含标签
# 设置 Git pull 默认使用变基
git config --global pull.rebase true # 设置 pull 默认使用变基
# 设置 Git 拉取配置
git config --global fetch.prune false # 默认不自动删除远程仓库中已删除的分支对应的本地引用
# 设置 Git 变基配置
git config --global rebase.autoStash false # 在变基时,不自动保存当前工作区的更改
git config --global rebase.autosquash false # 在变基时,不自动执行 --autosquash 功能
git config --global rebase.updateRefs false # 在变基时,不自动更新引用
git config --global rebase.backend merge # 指定变基的后端实现为 merge
# 设置 Git 安全目录配置
git config --global --unset-all safe.directory
git config --global safe.directory '*' --replace-all # 允许在任何目录中执行 Git 操作(谨慎使用)
git config --global safe.directory '.' --replace-all # 允许在当前目录中执行 Git 操作
# 设置 Git 日志时间戳为本地时区
git config --global i18n.logDate local # 设置日志时间戳为本地时区
# 启用颜色化的 diff 输出
git config --global color.diff auto
# 显示更多上下文
git config --global diff.context 3
# 显示函数名
git config --global diff.func true
# 2. 高级配置
# 忽略空白差异
git config --global diff.ignore-space-at-eol true
git config --global diff.ignore-space-change true
git config --global diff.ignore-all-space true
# 显示行号
git config --global diff.linenum true
# 显示更多文件名信息
git config --global diff.mnemonicprefix true
echo "Git 配置已完成!"