不使用 .gitconfig 设置 Git 用户名和电子邮件?

Setting Git username and email without using .gitconfig?

我有一个适用于 macOS 的点文件存储库,其中包含 .gitconfig 文件。我想将 usernameemail 与该文件分开,以便这些变量永远不会作为我的 public 点文件存储库的一部分提交。

我看到的一种方法是指定一个名为 .extra 的文件,将其保存在 ~/ 目录中(在 .bash_profile 中引用它)并设置 Git 变量,以便它们在我的 public 存储库外部,即我对 .gitconfig 所做的任何 public 更改将 包含usernameemail.

问题是 none 设置这些值的方法对我有用。每次尝试后,我都会收到以下消息:

Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly:

git config --global user.name "Your Name"

git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

我的 .extra 文件中有以下内容,但他没有工作:

GIT_AUTHOR_NAME="First Last"

GIT_COMMITTER_NAME="First Last"

EMAIL=email@example.com

我尝试了使用这些变量和其他变量的各种方法,包括和排除引号,甚至在这些行前面加上前缀 export。有点不知所措!

所有设置都可以在每个存储库的基础上进行设置:只需从 git config 命令中删除 --global 即可:

# from inside your repo :
git config user.name "Your Name"
git config user.email you@example.com

这些配置设置随后将存储在 .git/config 文件中(在 repo 文件夹中),并将优先于全局配置选项。

这适用于任何 git 配置选项(别名、颜色...)

注意 :您必须为您创建的任何新克隆或要使用此身份的任何其他回购重复配置

我找到了解决方案:

将以下内容添加到您的点文件的 .gitconfig 文件中:

[user] # These values are set in ~/.gitconfig_local

[include] path = ~/.gitconfig_local

~/ 中创建一个名为 .gitconfig_local 的文件,并在 .gitconfig 文件中添加您不想提交或制作的值 public。在这种情况下:

[user] name = First Last email = email@example.com

正如您可能猜到的那样,[include] 命令 "links" 到存储库外部的本地辅助配置文件,因此是私有的。 .gitconfig 中的其余设置(例如别名、颜色和编辑器设置等)仍将是 public。

另一种可能性是对大部分配置使用 $XDG_CONFIG_HOME/git/config(通常是 ~/.config/git/config)并将其包含在点文件存储库中。然后,您可以将 .gitconfig 添加到 .gitignore 并将其用于 user.nameuser.email 和其他您不想提交的值。

请参阅 git-config documentation 以了解其读取文件的顺序。