.gitconfig
通常存储在 user.home
目录中。
我在为公司A和公司B的项目工作时使用不同的身份(主要是姓名/电子邮件)。如何使用两个不同的Git配置,以便我的提交不会使用我的姓名/电子邮件?
.gitconfig
通常存储在 user.home
目录中。
我在为公司A和公司B的项目工作时使用不同的身份(主要是姓名/电子邮件)。如何使用两个不同的Git配置,以便我的提交不会使用我的姓名/电子邮件?
自 Git 版本 2.13 起,Git 支持条件配置包含。在此示例中,我们将 Company A 的存储库克隆到 ~/company_a
目录中,将 Company B 的存储库克隆到 ~/company_b
中。
在您的 .gitconfig
文件末尾,您可以添加以下内容:
[includeIf "gitdir:~/company_a/"]
path = .gitconfig-company_a
[includeIf "gitdir:~/company_b/"]
path = .gitconfig-company_b
.gitconfig-company_a
的示例内容(如果可以使用全局ssh密钥,则可以省略[core]
部分):
[user]
name = John Smith
email = john.smith@companya.net
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_companya
.gitconfig-company_b
的示例内容:
[user]
name = John Smith
email = js@companyb.com
[core]
sshCommand = ssh -i ~/.ssh/id_rsa_companyb
git config有三个级别:项目级别、全局级别和系统级别。
要创建一个特定于项目的配置,您必须在项目目录下执行此操作:
$ git config user.name "John Doe"
创建全局配置:
$ git config --global user.name "John Doe"
创建系统配置:$ git config --system user.name "John Doe"
正如您所猜测的那样,项目优先级高于全局,全局优先级高于系统。
注意:项目配置仅适用于此特定存储库的一个副本/克隆,并且如果从远程重新克隆干净的存储库,则需要重新应用它。它会更改一个本地文件,而不会使用提交/推送发送到远程。
git config user.name
或git config user.email
即可查看Git在当前仓库中使用的用户名或电子邮件。 - Abhishek Divekar一个特定的仓库副本中的 .git/config
文件只是针对该副本本地的。在那里放置的任何设置只会影响该项目的操作。
(默认情况下,git config
修改的是 .git/config
,而不是 ~/.gitconfig
,只有使用 --global
才会修改后者。)
感谢 @crea1
一个小变体:
正如在https://git-scm.com/docs/git-config#_includes上所写的:
如果模式以
/
结尾,则会自动添加**
。例如,模式foo/
变为foo/**
。换句话说,它递归地匹配foo
和其内部所有内容。
所以在我的情况下,我使用
~/.gitconfig :
[user] # as default, personal needs
email = myalias@personal-domain.fr
name = bcag2
[includeIf "gitdir:~/workspace/"] # job needs, like workspace/* so all included projects
path = .gitconfig-job
# all others section: core, alias, log…
如果项目目录在我的~/wokspace/
中,则默认用户设置将替换为
~/.gitconfig-job:
~/wokspace/
文件夹中,默认的用户设置会被替换为~/.gitconfig-job。[user]
name = John Smith
email = js@company.com
git config user.name
返回正确的用户名,那应该没问题了。你使用的是 GNU/Linux 还是其他操作系统? - bcag2gitdir
替换为 gitdir/i
。示例:[includeIf "gitdir/i:~/workspace/"]
- mskolnick具体来说,您还可以使用--local
选项来使用当前存储库配置文件:
git config --local user.name "John Doe"
正如@SherylHohman所提到的,使用以下方式在您的编辑器中打开本地文件:
git config --local --edit
git config --local --edit
命令,它会为你创建/打开本地文件,以便你可以直接进行编辑。这对我来说非常重要,因为由于某种未知的原因,无论是 VSCode 还是我的 XYPlorer 文件浏览器都不会显示我的 .git 文件夹(尽管其他点文件和系统文件都显示出来,而且传统上,如果我想要它显示我的 .git 文件夹,我的文件浏览器也会显示它)。对于我的情况,直接编辑文件比通过命令行添加多个选项要容易得多。 - SherylHohman另一种方法是使用direnv,并将配置文件按目录分开。例如:
.
├── companyA
│ ├── .envrc
│ └── .gitconfig
├── companyB
│ ├── .envrc
│ └── .gitconfig
└── personal
├── .envrc
└── .gitconfig
每个.envrc
文件应该包含类似于以下内容:
export GIT_CONFIG_GLOBAL=$(pwd)/.gitconfig
而.gitconfig
是具有所需值的通常gitconfig。
这就是我在自定义的.gitconfig
文件中实际拥有的内容:
[user]
email = my.name@company.com
[include]
path = ~/.gitconfig
这里只覆盖了 user.email
,其他配置都来自默认的 ~/.gitconfig
。
我正在以下方式为我的电子邮件而努力:
git config --global alias.hobbyprofile 'config user.email "me@example.com"'
然后,当我克隆一个新的工作项目时,我只需要运行git hobbyprofile
,它就会被配置为使用该电子邮件。
GIT_CONFIG
指向一个文件,git config
将使用该文件。使用 GIT_CONFIG=~/.gitconfig-A git config key value
命令可操作指定的文件。GIT_CONFIG_GLOBAL
。例如:GIT_CONFIG_GLOBAL=~/.gitconfig-another git commit -m "foo"
。 - Abdull/path/to/repo/.git/config
)来自定义项目的Git配置。顺便说一下, git config
默认情况下会写入此文件。cd /path/to/repo
git config user.name 'John Doe' # sets user.name locally for the repo
我喜欢为不同的项目创建单独的配置文件(例如在~/.gitconfig.d/
中),然后将它们包含到版本库的配置文件中:
cd /path/to/repo
git config include.path '~/.gitconfig.d/myproject.conf'
如果您需要在属于一个项目的多个存储库中使用相同的选项集,那么这样做效果很好。您还可以设置shell别名或自定义Git命令来操作配置文件。
自2021年8月13日起,git不再支持HTTPs身份验证方法,因此我认为需要更新此答案。
按照以下步骤操作:
~/.ssh/
中的Git的所有SSH密钥(公钥和私钥)。ssh-keygen -t rsa -b 4096 -C "account1@gmail.com"
ssh-keygen -t rsa -b 4096 -C "account2@gmail.com"
当要求文件名时,分别为account1和account2指定默认值~/.ssh/id_rsa
和~/.ssh/id_rsa_acc2
。
eval `ssh-agent`
ssh-add -k ~/.ssh/id_rsa
ssh-add -k ~/.ssh/id_rsa_acc2
确认密钥已通过命令ssh-add -l
添加
复制账户1和账户2的公钥并将其添加到您的GitHub帐户。 命令:cat ~/.ssh/id_rsa.pub | pbcopy
(检查是否已复制:pbpaste
)
将账户1的用户名和用户电子邮件设置为全局GitHub配置:
git config --global user.name "acc1_username"
git config --global user.email "account1@gmail.com"
~/.ssh/config
的文件,并添加以下配置:# account1 github
Host github.com
HostName github.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa
# account2 github
Host github.com-acc2
HostName github.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa_acc2
git config user.name "acc2_username"
git config user.email "account2@gmail.com"
# for account1 repo
git remote set-url origin git@github.com:acc1_username/reponame.git
# for account2 repo
git clone git@github.com-acc2:acc2_username/reponame.git
如有任何疑问,请随时添加评论。
gitdir
文件夹必须以斜杠(/
)结尾,否则git
将会忽略ignoreIf
配置(实际上会导致结果为false
)。 - tukusejssirs~/company_a/repo_1
)。如果您位于非存储库目录(如~/company_a
)并运行git config user.email
,您仍将获得全局电子邮件地址。不要让这使您困惑。 - ADTC