.gitconfig
通常存放在user.home
目录中。
我使用不同的身份为 A 公司处理项目,为 B 公司处理其他项目(主要是姓名/电子邮件)。我怎样才能拥有两种不同的 Git 配置,以便我的签到不使用姓名/电子邮件?
.gitconfig
通常存放在user.home
目录中。
我使用不同的身份为 A 公司处理项目,为 B 公司处理其他项目(主要是姓名/电子邮件)。我怎样才能拥有两种不同的 Git 配置,以便我的签到不使用姓名/电子邮件?
git config 有 3 个级别;项目、全局和系统。
创建一个项目特定的配置,你必须在项目目录下执行:
$ git config user.name "John Doe"
创建一个全局配置:
$ git config --global user.name "John Doe"
创建系统配置:
$ git config --system user.name "John Doe"
正如您可能猜到的那样,项目覆盖全局和全局覆盖系统。
注意:项目配置对于这个特定 repo 的一个特定副本/克隆来说是本地的,如果 repo 从远程重新克隆干净,则需要重新应用。它通过提交/推送更改未发送到远程的本地文件。
从 git 版本 2.13 开始,git 支持条件配置包括. 在此示例中,我们将公司 A 的存储库克隆到~/company_a
目录中,将公司 B 的存储库克隆到~/company_b
.
在你的.gitconfig
你可以放这样的东西。
[includeIf "gitdir:~/company_a/"]
path = .gitconfig-company_a
[includeIf "gitdir:~/company_b/"]
path = .gitconfig-company_b
.gitconfig-company_a 的示例内容(如果可以使用全局 ssh 密钥,可以省略核心部分)
[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
修改.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…
因此,如果项目目录位于 my 中~/wokspace/
,则默认用户设置将替换为
~/.gitconfig-job:
[user]
name = John Smith
email = js@company.com
明确地说,您还可以--local
使用当前存储库配置文件:
git config --local user.name "John Doe"
或者正如@SherylHohman 提到的,使用以下命令在编辑器中打开本地文件:
git config --local --edit
我正在通过以下方式为我的电子邮件执行此操作:
git config --global alias.hobbyprofile 'config user.email "me@example.com"'
然后,当我克隆一个新的工作项目时,我只需运行git hobbyprofile
它,它将被配置为使用该电子邮件。
另一种方法是使用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
的文件。git config
随着GIT_CONFIG=~/.gitconfig-A git config key value
指定的文件被操纵。
您可以通过更改存储库特定配置文件(即/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
我更喜欢为不同的项目(例如 in ~/.gitconfig.d/
)创建单独的配置文件,然后将它们包含在存储库的配置文件中:
cd /path/to/repo
git config include.path '~/.gitconfig.d/myproject.conf'
如果您需要在属于单个项目的多个存储库中使用相同的选项集,这很有效。您还可以设置 shell 别名或自定义 Git 命令来操作配置文件。
从系统中查找 .gitconfig
Windows 的文件位置: “C:\Users${USER_NAME}.gitconfig”
Linux 的文件位置: “/usr/local/git/etc/gitconfig”
打开 .gitconfig 文件并根据您的条件添加以下行
[includeIf "gitdir:D:\ORG-A-PROJECTS\"]
[user]
name = John Smith
email = js@organizationx.com [includeIf "gitdir:~/organization_b/"]
[user]
name = John Doe
email = jd@organizationy.com
我在同一条船上。我写了一个小 bash 脚本来管理它们。 https://github.com/thejeffreystone/setgit
#!/bin/bash
# setgit
#
# Script to manage multiple global gitconfigs
#
# To save your current .gitconfig to .gitconfig-this just run:
# setgit -s this
#
# To load .gitconfig-this to .gitconfig it run:
# setgit -f this
#
#
#
# Author: Jeffrey Stone <thejeffreystone@gmail.com>
usage(){
echo "$(basename $0) [-h] [-f name]"
echo ""
echo "where:"
echo " -h Show Help Text"
echo " -f Load the .gitconfig file based on option passed"
echo ""
exit 1
}
if [ $# -lt 1 ]
then
usage
exit
fi
while getopts ':hf:' option; do
case "$option" in
h) usage
exit
;;
f) echo "Loading .gitconfig from .gitconfig-$OPTARG"
cat ~/.gitconfig-$OPTARG > ~/.gitconfig
;;
*) printf "illegal option: '%s'\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
尝试进行git stash
本地更改时出错。来自 git 的错误说“请告诉我你是谁”,然后告诉我“运行git config --global user.email "you@example.com
并git config --global user.name "Your name"
设置您帐户的默认身份”。但是,您必须省略 --global以仅在当前存储库中设置身份。
2021 年 8 月 13 日之后,git 不支持 HTTPS 身份验证方法,所以我相信这个答案需要更新。
请按照以下步骤操作:
~/.ssh/
。ssh-keygen -t rsa -b 4096 -C "account1@gmail.com"
ssh-keygen -t rsa -b 4096 -C "account2@gmail.com"
当询问文件名时,分别~/.ssh/id_rsa
为 account1 和~/.ssh/id_rsa_acc2
account2 提供默认值。
eval `ssh-agent -s
ssh-add -k ~/.ssh/id_rsa
ssh-add -k ~/.ssh/id_rsa_acc2
确认键是通过命令添加的ssh-add -l
复制 account1 和 account2 的公钥并将其添加到您的 github 帐户。
将 account1 的用户名和用户电子邮件设置为全局 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
如有任何疑问,请随时添加评论。