feat: split git user identity by repo directory

Leave global name/email empty with useConfigOnly so unmatched repos cannot commit. Personal identity applies under ~/.dotfile, ~/code, and ~/.pouch; work identity stays in local ~/.gitconfig.work for ~/workrepo.
This commit is contained in:
2026-09-14 14:55:44 +08:00
parent 097fce7827
commit 0b04a4de50
4 changed files with 35 additions and 3 deletions
+4
View File
@@ -61,6 +61,10 @@ ff reload # 重载 fish 配置
## Git 配置
- 公共配置在 `git/.gitconfig`
- **身份**:不设全局 `user.name` / `user.email`,`user.useConfigOnly = true`。未匹配目录时拒绝 commit,避免错用身份
- `~/.dotfile/`、`~/code/`、`~/.pouch/` → `git/.gitconfig.personal`(个人,进仓库,已写 name/email)
- `~/workrepo/` → `~/.gitconfig.work`(公司,仅本机;`git-init` 会生成空模板)
- include 目标文件必须存在,否则该目录下 git 命令会 fatal
- **凭据**(所有 Git 版本):`git/git-credential.sh` 写在主配置里,使用 `!$HOME/.dotfile/...`(Git 不展开 `~`)
- macOS:`osxkeychain` → 回退 `store`(`~/.git-credentials`)
- Linux:`libsecret` → 回退 `store`
+12 -3
View File
@@ -1,6 +1,7 @@
# 不设全局 user.name / user.email,避免未匹配目录误用身份。
# useConfigOnly(Git 2.8+):未配置时拒绝 commit,而不是猜系统用户名。
[user]
name = laily
email = i@laily.net
useConfigOnly = true
[core]
autocrlf = input
quotepath = false
@@ -42,8 +43,16 @@
[mergetool "nvimdiff"]
cmd = nvim -d \"$LOCAL\" \"$REMOTE\" -ancestor \"$BASE\" -merge \"$MERGED\"
trustExitCode = true
# 按仓库目录套身份。个人身份在 git/.gitconfig.personal;公司邮箱只放本机 ~/.gitconfig.work。
# include 目标文件必须存在,否则该目录下 git 命令会直接 fatal。
[includeIf "gitdir:~/.dotfile/"]
path = ~/.dotfile/git/.gitconfig.personal
[includeIf "gitdir:~/code/"]
path = ~/.dotfile/git/.gitconfig.personal
[includeIf "gitdir:~/.pouch/"]
path = ~/.dotfile/git/.gitconfig.personal
[includeIf "gitdir:~/workrepo/"]
path = ~/.workgitconfig
path = ~/.gitconfig.work
[rebase]
autoSquash = true
autoStash = true
+4
View File
@@ -0,0 +1,4 @@
# 个人身份:~/.dotfile、~/code、~/.pouch(由 includeIf gitdir 加载)
[user]
name = laily
email = i@laily.net
+15
View File
@@ -11,6 +11,7 @@ usage() {
备份已有 ~/.gitconfig,软链接到 ~/.dotfile/git/.gitconfig。
同时为 git-credential.sh 添加可执行权限。
若本机还没有 ~/.gitconfig.work,则生成空模板(~/workrepo 公司身份,需自行填写)。
凭据由 ~/.dotfile/git/git-credential.sh 处理(兼容 Git < 2.36,低版本回退 store)。
@@ -41,3 +42,17 @@ fi
chmod +x "$HOME/.dotfile/git/git-credential.sh"
# 兼容旧路径:若仍存在则保持可执行
chmod +x "$HOME/.dotfile/git/git-credential-linux.sh" 2>/dev/null || true
work_cfg="$HOME/.gitconfig.work"
if [ -e "$work_cfg" ]; then
echo " $work_cfg already exists, skip"
else
cat > "$work_cfg" <<'EOF'
# 公司 Git 身份(仅本机,不要提交到 dotfile)
# ~/workrepo 下的仓库会加载本文件。取消注释并填写后再 commit:
# [user]
# name = Your Name
# email = you@company.com
EOF
echo " Created $work_cfg — fill in company user.name / user.email"
fi