在我的~/。gitconfig,我列出我的个人电子邮件地址下[用户],因为这是我想用于Github回购。
但是,我最近也开始在工作中使用git。我公司的git回购允许我提交,但是当它发出新的变更集通知时,它说它们来自匿名,因为它不识别我的.gitconfig中的电子邮件地址——至少,这是我的理论。
是否可以在.gitconfig中指定多个[用户]定义?或者是否有其他方法覆盖特定目录的默认.gitconfig ?在我的情况下,我检查了~/worksrc/中的所有工作代码-是否有一种方法只为该目录(及其子目录)指定.gitconfig ?
本地邮箱/邮箱/ bashrc
.bashrc_local:不要跟踪这个文件,只把它放在你的工作电脑上:
export GIT_AUTHOR_EMAIL='me@work.com'
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
.bashrc:跟踪这个文件,让它在工作电脑和家用电脑上都一样:
F="$HOME/.bashrc_local"
if [ -r "$F" ]; then
. "$F"
fi
我正在使用https://github.com/technicalpickles/homesick来同步我的dotfiles。
如果只有git配置可以接受环境变量:git配置中的Shell变量扩展
这个答案部分是受到@Saucier的帖子的启发,但我正在寻找一种自动设置user.name和user的方法。基于远程的,每回购一次的电子邮件,这比他开发的git-passport包要轻一些。也h/t @John为useConfigOnly设置。以下是我的解决方案:
.gitconfig变化:
[github]
name = <github username>
email = <github email>
[gitlab]
name = <gitlab username>
email = <gitlab email>
[init]
templatedir = ~/.git-templates
[user]
useConfigOnly = true
Post-checkout钩子应该保存到以下路径:~/.git-templates/hooks/ Post-checkout:
#!/usr/bin/env bash
# make regex matching below case insensitive
shopt -s nocasematch
# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
'github'
'gitlab'
)
set_local_user_config() {
local service="${1}"
local config="${2}"
local service_config="$( git config --get ${service}.${config} )"
local local_config="$( git config --get --local user.${config} )"
if [[ "${local_config}" != "${service_config}" ]]; then
git config --local "user.${config}" "${service_config}"
echo "repo 'user.${config}' has been set to '${service_config}'"
fi
}
# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
if [[ "${remote_url}" =~ "${s}" ]]; then
set_local_user_config "${s}" 'name'
set_local_user_config "${s}" 'email'
break
fi
done
我对github和gitlab使用不同的凭据,但上面代码中的那些引用可以用你使用的任何服务替换或增强。为了让签出后钩子自动在本地为签出后的回购设置用户名和电子邮件,确保服务名称出现在远程url中,将其添加到签出后脚本中的services数组中,并在.gitconfig中为它创建一个包含该服务的用户名和电子邮件的部分。
如果远程url中没有出现任何服务名称,或者repo没有远程用户名和电子邮件将不会在本地设置。在这些情况下,用户。useConfigOnly设置将在发挥,将不允许您提交,直到用户名和电子邮件设置为回购级别,并将提示用户配置该信息。
也许这是一个简单的黑客,但它是有用的。只需生成如下所示的2个ssh密钥。
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/GowthamSai/.ssh/id_rsa): work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in damsn.
Your public key has been saved in damsn.pub.
The key fingerprint is:
SHA256:CrsKDJWVVek5GTCqmq8/8RnwvAo1G6UOmQFbzddcoAY GowthamSai@Gowtham-MacBook-Air.local
The key's randomart image is:
+---[RSA 4096]----+
|. .oEo+=o+. |
|.o o+o.o= |
|o o o.o. + |
| =.+ . = |
|= *+. S. |
|o*.++o . |
|=.oo.+. |
| +. +. |
|.o=+. |
+----[SHA256]-----+
同样的方法为个人创造一个。因此,您有两个ssh密钥,work和company。复制工作。酒吧,工作,个人。Pub, personal to ~/。ssh /目录。
然后使用以下代码创建一个shell脚本,并将其命名为crev.sh (Company Reverse),并包含以下内容。
cp ~/.ssh/work ~/.ssh/id_rsa
cp ~/.ssh/work.pub ~/.ssh/id_rsa.pub
以同样的方式,用以下内容创建一个名为prew .sh(个人反向)的文件。
cp ~/.ssh/personal ~/.ssh/id_rsa
cp ~/.ssh/personal.pub ~/.ssh/id_rsa.pub
在~ /。Bashrc为这些脚本添加别名如下所示
alias crev="sh ~/.ssh/crev.sh"
alias prev="sh ~/.ssh/prev.sh"
source ~/.bashrc
无论何时你想使用company,只要使用crev,如果你想使用personal,请使用prev:-p。
将这些ssh密钥添加到您的GitHub帐户。请确保之前没有生成id_rsa,因为这些脚本将覆盖id_rsa。如果您已经生成了id_rsa,请将其用于其中一个帐户。将它们复制为个人密钥,跳过生成个人密钥。
在看完所有的答案后,这就是我的解决方案:
场景:
本地操作系统:Linux (bash)
GitHub上的两个账户(工作/个人)
不同密钥的SSH访问
我的工作帐户将是“主”帐户(因此不需要自定义配置)
配置:
Set a "fake" host, which allows me to select the proper SSH keypair.
At ~/.ssh/config:
# Personal GitHub account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
IdentitiesOnly yes
"Tell" git it should use a custom configuration within a given directory subtree:
At ~/.gitconfig:
# My personal projects
[includeIf "gitdir/i:~/REPOS/PERSONAL/"]
path = ~/REPOS/PERSONAL/personal.gitconfig
Finally, my config at ~/REPOS/PERSONAL/personal.gitconfig:
# gitconfig for personal projects
[user]
email = personalemail@example.com
[url "git@github-personal"]
insteadOf = git@github.com
一旦上述三个到位,我可以克隆一个个人回购像这样:
user@host:~/REPOS/PERSONAL$ git clone git@github.com:jmnavarrol/python-multigit.git
Cloning in 'python-multigit'...
(...)
user@host:~/REPOS/PERSONAL$ cd python-multigit && git remote -v
origin git@github-personal:jmnavarrol/python-multigit.git (fetch)
origin git@github-personal:jmnavarrol/python-multigit.git (push)
user@host:~/REPOS/PERSONAL/python-multigit$
在~/REPOS/PERSONAL/ PERSONAL .gitconfig添加了url重写…
[url "git@github-personal"]
insteadOf = git@github.com
...是什么允许我仅通过脚本中的“标准”URL引用repo, subrepos(即python-multigit本身的情况-是的,蹩脚的自我推销尝试),而不用冒使用“错误”URL的风险,因此,错误的身份验证。
我的意见。