GitHub 配置与协作实践清单


初始化:账号与 SSH Key

  1. 本地生成密钥:
ssh-keygen -t ed25519 -C "you@example.com"
  1. ~/.ssh/id_ed25519.pub 上传至 GitHub → Settings → SSH and GPG keys;
  2. ~/.ssh/config 中建立快捷配置:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
PreferredAuthentications publickey
  1. 验证连通性:ssh -T git@github.com

仓库管理与常用设置

  • 全局用户信息
git config --global user.name "linda1729"
git config --global user.email "you@example.com"
git config --global core.autocrlf input
git config --global pull.rebase false
  • 模板化忽略文件:在用户目录创建 ~/.config/git/ignore,添加常见 IDE、系统文件,并执行:
git config --global core.excludesfile ~/.config/git/ignore
  • 保护默认分支:在 GitHub 仓库 Settings → Branches 中启用 Require a pull request before mergingRequire status checks,防止直接推送到 main

提交与分支策略

  1. 命名约定
    • 功能分支:feature/<module>-<keyword>
    • 修复分支:fix/<issue>-<keyword>
    • 文档分支:docs/<topic>
  2. 提交信息模版.gitmessage):
<type>: <subject>

<body>

在全局配置中启用:

git config --global commit.template ~/.gitmessage

常用 type:feat、fix、docs、refactor、chore。
3. Rebase 与合并:使用 git pull --rebase 保持线性历史;合并前检查 git status 确认无杂项文件。

协作中的安全与自动化

  • Personal Access Token (PAT):在 Actions 或命令行中需要 HTTPS 推送时,使用细粒度 Token,限制仓库与权限范围。
  • GitHub CLI:安装 gh 后可快速创建 PR:
gh pr create --fill --web
  • Issue 模板:在 .github/ISSUE_TEMPLATE 中准备 bug-report.ymlfeature-request.yml,提升反馈质量。
  • Code Owners:通过 .github/CODEOWNERS 指定必审人:
*       @linda1729
docs/* @docs-team

常见问题速记

  • 仓库过大:执行 git gcgit lfs migrate 清理历史文件。
  • 误提交敏感信息:立刻 git revertgit filter-repo --invert-paths --path secrets.txt,并在 GitHub 后台重置 Tokens。
  • 多账号共存:使用 Host github-workIdentityFile ~/.ssh/id_ed25519_work 区分不同身份。

保持规范的 GitHub 配置,能大幅降低多人协作的沟通与排错成本。把以上脚本和模板放进 dotfiles 仓库,迁移到新设备时只需一条命令即可恢复。


Author: linda1729
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source linda1729 !
评论
  TOC