以 root 身份提交时如何使用我的个人 git 配置?

How do I use my personal git config when committing as root?

我使用 etckeeper 来控制我的 /etc 目录。

由于/etc下文件的所有权,我需要运行git作为root。

我怎样才能做到这一点并且仍然可以轻松访问 所有 我心爱的 git 别名和其他配置?

git -c include.path=<file> 将包含 <file> 中的配置。

为了自动提取非 root 用户的配置文件,我使用 bash 别名:

alias gsudo='sudo git -c "include.path='"${XDG_CONFIG_DIR:-$HOME/.config}/git/config\" -c \"include.path=$HOME/.gitconfig\""

然后我对两者都使用 gsudo 而不是 git

  • 运行 作为 root
  • 可以访问所有非root用户git配置

检查是否确实正在导入配置:

gsudo config --list --show-origin --includes | less

当然,如果你有任何不安全的配置,你应该小心。

也许同时拥有 "safe" 和 "potentially dangerous" 配置文件是可行的方法。用户的 ~/.config/git/config 可能是 "potentially dangerous" 一个,其中 [include] 是 "safe" 仅限 root 的配置文件(这是 gsudo 别名中唯一使用的配置文件) .