Bro*_*and 689 git git-config
在我~/.gitconfig,我列出我的个人电子邮件地址[user],因为这是我想用于Github回购.
但是,我最近也开始使用git工作.我公司的git repo允许我提交,但是当它发出新变更集的公告时,它说它们来自匿名,因为它不能识别我的电子邮件地址.gitconfig- 至少,这是我的理论.
是否可以指定多个[user]定义.gitconfig?或者是否有其他方法来覆盖.gitconfig某个目录的默认值?在我的情况下,我检查所有工作代码~/worksrc/- 有没有办法只指定一个.gitconfig目录(及其子目录)?
dis*_*ray 932
您可以将单个仓库配置为使用覆盖全局配置的特定用户/电子邮件地址.从repo的根目录开始运行
git config user.name "Your Name Here"
git config user.email your@email.com
Run Code Online (Sandbox Code Playgroud)
而在〜/ .gitconfig中配置默认用户/电子邮件
git config --global user.name "Your Name Here"
git config --global user.email your@email.com
Run Code Online (Sandbox Code Playgroud)
Tom*_*šek 399
从git 2.13开始,可以使用新引入的条件包含来解决这个问题.
一个例子:
全局配置〜/ .gitconfig
[user]
name = John Doe
email = john@doe.tld
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
Run Code Online (Sandbox Code Playgroud)
特定于工作的配置〜/ work/.gitconfig
[user]
email = john.doe@company.tld
Run Code Online (Sandbox Code Playgroud)
Rah*_*sad 103
或者您可以在本地.git/config文件中添加以下信息
[user]
name = Your Name
email = your.email@gmail.com
Run Code Online (Sandbox Code Playgroud)
Kal*_*lah 44
一个命令github帐户切换
此解决方案采用单个git别名的形式.执行后,当前项目用户将被附加到另一个帐户
生成ssh密钥
ssh-keygen -t rsa -C "rinquin.arnaud@gmail.com" -f '/Users/arnaudrinquin/.ssh/id_rsa'
[...]
ssh-keygen -t rsa -C "arnaud.rinquin@wopata.com" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'
Run Code Online (Sandbox Code Playgroud)
将它们链接到您的GitHub/Bitbucket帐户
pbcopy < ~/.ssh/id_rsa.pubadd SSH keygithub页面中pbcopy < ~/.ssh/id_rsa_pro.pub步骤1.自动ssh键切换.
我们可以ssh根据具体情况配置发送使用特定加密密钥host.好的是你可以有几个别名hostname.
看到这个示例~/.ssh/config文件:
# Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# Professional github alias
Host github_pro
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_pro
Run Code Online (Sandbox Code Playgroud)
git远程配置
您现在可以通过更改git@github.com方式在git遥控器中使用这些别名git@github_pro.
您可以更改现有的项目遥控器(使用类似的东西git remote set-url origin git@github_pro:foo/bar.git)或在克隆它们时直接调整它们.
git clone git@github.com:ArnaudRinquin/atom-zentabs.git
Run Code Online (Sandbox Code Playgroud)
使用别名,它变成:
git clone git@github_pro:ArnaudRinquin/atom-zentabs.git
步骤2.更改git user.email
Git配置设置可以是全局的,也可以是每个项目.在我们的例子中,我们想要每个项目设置.它很容易改变它:
git config user.email 'arnaud.rinquin@wopata.com'
Run Code Online (Sandbox Code Playgroud)
虽然这很容易,但我们的开发人员需要很长时间.我们可以为此编写一个非常简单的git别名.
我们将把它添加到~/.gitconfig文件中.
[user]
name = Arnaud Rinquin
email = rinquin.arnaud@gmail.com
...
[alias]
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
Run Code Online (Sandbox Code Playgroud)
然后,我们所要做的就是git setpromail只为这个项目更改我们的电子邮件.
第3步.请指一个命令开关?!
使用单个无参数命令从默认帐户切换到指定帐户不是很好吗?这绝对是可能的.此命令将包含两个步骤:
我们已经有第二步的一个命令解决方案,但第一步更难.一个命令远程主机更改
这里有另一个git alias命令形式的解决方案,可以添加到您的~/.gitconfig:
[alias]
changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"
Run Code Online (Sandbox Code Playgroud)
这允许将所有遥控器从一个主机更改为另一个主机(别名).看例子:
$ > git remote -v
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (push)
$ > git changeremotehost github.com github_pro
$ > git remote -v
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (push)
Run Code Online (Sandbox Code Playgroud)
把它们结合起来
我们现在只需将两个命令合并为一个,这很容易.看看我如何集成bitbucket主机切换.
[alias]
changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
gopro = !sh -c \"git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail\"
Run Code Online (Sandbox Code Playgroud)
Sau*_*ier 39
在从Orr Sella的博客文章中获得灵感后,我写了一个预提交钩子(驻留~/.git/templates/hooks),它将根据本地存储库中的信息设置特定的用户名和电子邮件地址./.git/config:
您必须将模板目录的路径放入~/.gitconfig:
[init]
templatedir = ~/.git/templates
Run Code Online (Sandbox Code Playgroud)
然后每个git init或git clone将拾取该钩子并将在下一个应用用户数据git commit.如果你想将钩子应用于已经存在的repos,那么只需git init在repo中运行一个以重新初始化它.
这是我提出的钩子(它仍然需要一些抛光 - 欢迎提出建议).保存为
~/.git/templates/hooks/pre_commit
Run Code Online (Sandbox Code Playgroud)
要么
~/.git/templates/hooks/post-checkout
Run Code Online (Sandbox Code Playgroud)
并确保它是可执行的: chmod +x ./post-checkout || chmod +x ./pre_commit
#!/usr/bin/env bash
# -------- USER CONFIG
# Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
git_remotes[0]="Github"
git_remotes[1]="Gitlab"
# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"
local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"
local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"
# -------- FUNCTIONS
setIdentity()
{
local current_id local_id
current_id[0]="$(git config --get --local user.name)"
current_id[1]="$(git config --get --local user.email)"
local_id=("$@")
if [[ "${current_id[0]}" == "${local_id[0]}" &&
"${current_id[1]}" == "${local_id[1]}" ]]; then
printf " Local identity is:\n"
printf "» User: %s\n» Mail: %s\n\n" "${current_id[@]}"
else
printf "» User: %s\n» Mail: %s\n\n" "${local_id[@]}"
git config --local user.name "${local_id[0]}"
git config --local user.email "${local_id[1]}"
fi
return 0
}
# -------- IMPLEMENTATION
current_remote_url="$(git config --get --local remote.origin.url)"
if [[ "$current_remote_url" ]]; then
for service in "${git_remotes[@]}"; do
# Disable case sensitivity for regex matching
shopt -s nocasematch
if [[ "$current_remote_url" =~ $service ]]; then
case "$service" in
"${git_remotes[0]}" )
printf "\n»» An Intermission\n» %s repository found." "${git_remotes[0]}"
setIdentity "${local_id_0[@]}"
exit 0
;;
"${git_remotes[1]}" )
printf "\n»» An Intermission\n» %s repository found." "${git_remotes[1]}"
setIdentity "${local_id_1[@]}"
exit 0
;;
* )
printf "\n» pre-commit hook: unknown error\n» Quitting.\n"
exit 1
;;
esac
fi
done
else
printf "\n»» An Intermission\n» No remote repository set. Using local fallback identity:\n"
printf "» User: %s\n» Mail: %s\n\n" "${local_fallback_id[@]}"
# Get the user's attention for a second
sleep 1
git config --local user.name "${local_fallback_id[0]}"
git config --local user.email "${local_fallback_id[1]}"
fi
exit 0
Run Code Online (Sandbox Code Playgroud)
编辑:
所以我把钩子重写为Python中的钩子和命令.另外,也可以将脚本称为Git命令(git passport).此外,还可以在configfile(~/.gitpassport)中定义任意数量的ID,这些ID 可在提示中选择.你可以在github.com找到这个项目:git-passport - 用Python编写的Git命令和钩子来管理多个Git帐户/用户身份.
Joh*_*ohn 25
如果您不想拥有默认电子邮件地址(电子邮件地址链接到github用户),您可以配置要询问的内容.如何做到这一点取决于你使用的git版本,见下文.
(预期)缺点是您必须为每个存储库配置一次您的电子邮件地址(和您的姓名).所以,你不能忘记这样做.
[user]
name = Your name
email = "(none)"
Run Code Online (Sandbox Code Playgroud)
在你的全球配置中,~/.gitconfig如Dan Aloni在Orr Sella博客文章中的评论中所述.当尝试在存储库中进行第一次提交时,git失败并显示以下消息:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got '(none)')
Run Code Online (Sandbox Code Playgroud)
当在本地设置电子邮件地址时,该名称取自全局配置(该消息不完全准确).
版本<2.7.0中的行为不是用2.7.0修复的.您仍然可以使用Orr Sella博客文章中描述的预提交挂钩.此解决方案也适用于其他版本,但其他解决方案不适用于此版本.
Dan Aloni添加了一个实现该行为的选项(请参阅发行说明).使用它:
[user]
useConfigOnly = true
Run Code Online (Sandbox Code Playgroud)
要使其工作,您可能无法在全局配置中提供名称或电子邮件地址.然后,在第一次提交时,您会收到一条错误消息
fatal: user.useConfigOnly set but no name given
Run Code Online (Sandbox Code Playgroud)
因此消息不是很有启发性,但是由于您明确设置了选项,因此您应该知道该怎么做.与版本<2.7.0的解决方案相比,您始终必须手动设置名称和电子邮件.
Rob*_*b W 18
git使用多个名称/电子邮件的
另一个选项是使用别名git并使用该-c标志来覆盖全局和特定于存储库的配置.
例如,通过定义别名:
alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'
Run Code Online (Sandbox Code Playgroud)
要查看它是否有效,只需键入git config user.email:
$ git config user.email
name@example.com
Run Code Online (Sandbox Code Playgroud)
您也可以git在自己的内容中放置自定义可执行文件,而不是别名$PATH.
#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"
Run Code Online (Sandbox Code Playgroud)
这些方法优于特定于存储库的优点.git/config是,git当自定义git程序处于活动状态时,它适用于每个存储库.通过这种方式,您可以轻松地在用户/名称之间切换,而无需修改任何(共享)配置.
hIp*_*pPy 18
使用Git 2.13中的条件包含,现在可以在一台机器上共存多个用户/电子邮件而几乎没有工作.
user.gitconfig有我的个人姓名和电子邮件.work-user.gitconfig有我的工作名称和电子邮件.两个文件都在~路径上.
所以我的个人姓名/电子邮件默认适用.对于c:/work/目录,我的工作名称/电子邮件已应用.对于c:/work/github/dir,我的个人姓名/电子邮件已应用.这适用于应用的最后一个设置.
# ~/.gitconfig
[include]
path = user.gitconfig
[includeIf "gitdir/i:c:/work/"]
path = work-user.gitconfig
[includeIf "gitdir/i:c:/work/github/"]
path = user.gitconfig
Run Code Online (Sandbox Code Playgroud)
gitdir区分大小写和gitdir/i不区分大小写.
"gitdir/i:github/"将条件包括github在其路径中的任何目录中.
小智 18
阅读完所有答案后,这就是我提出的解决方案:
设想:
配置:
~/.ssh/config:
# Personal GitHub account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)
~/.gitconfig:
# My personal projects
[includeIf "gitdir/i:~/REPOS/PERSONAL/"]
path = ~/REPOS/PERSONAL/personal.gitconfig
Run Code Online (Sandbox Code Playgroud)
~/REPOS/PERSONAL/personal.gitconfig:
# gitconfig for personal projects
[user]
email = personalemail@example.com
[url "git@github-personal"]
insteadOf = git@github.com
Run Code Online (Sandbox Code Playgroud)
一旦上述三项就位,我就可以像这样克隆一个个人存储库:
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$
Run Code Online (Sandbox Code Playgroud)
添加 url 重写~/REPOS/PERSONAL/personal.gitconfig...
[url "git@github-personal"]
insteadOf = git@github.com
Run Code Online (Sandbox Code Playgroud)
...这使得我可以通过脚本、子存储库中的“标准” URL 来引用存储库(即python-multigit 本身的情况- 是的,有点自我推销的蹩脚尝试),而不必冒使用“错误”的风险因此,URL 的身份验证错误。
我的两分钱。
小智 11
git别名(和git configs中的部分)来救援!
添加别名(从命令行):
git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; :'
Run Code Online (Sandbox Code Playgroud)
然后,设置,例如
git config --global user.github.name "your github username"
git config --global user.github.email your@github.email
Run Code Online (Sandbox Code Playgroud)
在新的或克隆的仓库中,您可以运行此命令:
git identity github
Run Code Online (Sandbox Code Playgroud)
此解决方案不是自动的,但在全局〜/ .gitconfig中取消设置用户和电子邮件会强制git提醒您在每个新的或克隆的repo中手动设置它们.
git config --global --unset user.name
git config --global --unset user.email
git config --global user.useConfigOnly true
Run Code Online (Sandbox Code Playgroud)
有一个简单的解决方案似乎可以很好地避免错误.
只需[user]从您的部分中删除该部分~/.gitconfig,这将阻止您在未设置user.name每个存储库的情况下进行任何提交.
在你的~/.bashrc,为用户和电子邮件添加一些简单的别名:
alias ggmail='git config user.name "My Name";git config user.email me@gmail.com'
alias gwork='git config user.name "My Name";git config user.email me@work.job'
Run Code Online (Sandbox Code Playgroud)
这个答案部分受到了@Saucier的帖子的启发,但是我正在寻找一种基于遥控器设置user.name和user.email基于每个回购的自动方式,这比他开发的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
Run Code Online (Sandbox Code Playgroud)
post-checkout hook应该保存到以下路径~/.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
Run Code Online (Sandbox Code Playgroud)
我为github和gitlab使用不同的凭据,但上面代码中的那些引用可以使用您使用的任何服务进行替换或扩充.为了让post-checkout挂钩在结账后自动在本地设置用户名和电子邮件以确保服务名称出现在远程URL中,将其添加到post-checkout脚本中的services数组并为其创建一个部分.您的.gitconfig那个包含您的用户名和该服务的电子邮件.
如果远程URL中没有显示任何服务名称,或者repo没有远程服务器,则不会在本地设置用户名和电子邮件.在这些情况下,user.useConfigOnly设置将处于播放状态,在用户名和电子邮件设置为repo级别之前,将不允许您进行提交,并将提示用户配置该信息.
这是在阅读许多答案之后的完整步骤
如何为不同的github帐户设置多个SSH密钥设置
您可能要开始检查当前保存的密钥
$ ssh-add -l
Run Code Online (Sandbox Code Playgroud)
如果您决定在此之前删除所有缓存的键(可选,请谨慎操作)
$ ssh-add -D
Run Code Online (Sandbox Code Playgroud)
然后,您可以创建一个链接到您希望/需要使用的每个电子邮件/帐户的ssh发布/私钥。
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "work@company.com" <-- save it as "id_rsa_work"
$ ssh-keygen -t rsa -C "pers@email.com" <-- save it as "id_rsa_pers"
Run Code Online (Sandbox Code Playgroud)
执行此命令后,将创建以下文件
~/.ssh/id_rsa_work
~/.ssh/id_rsa_work.pub
~/.ssh/id_rsa_pers
~/.ssh/id_rsa_pers.pub
Run Code Online (Sandbox Code Playgroud)
确保身份验证代理正在运行
$ eval `ssh-agent -s`
Run Code Online (Sandbox Code Playgroud)
添加生成的密钥,如下所示(从〜/ .ssh文件夹)
$ ssh-add id_rsa_work
$ ssh-add id_rsa_pers
Run Code Online (Sandbox Code Playgroud)
现在,您可以再次检查保存的密钥
$ ssh-add -l
Run Code Online (Sandbox Code Playgroud)
现在,您需要将生成的公共密钥添加到您的github / bickbuket服务器中Acces Keys
将每个存储库克隆到不同的文件夹
转到用户工作所在的文件夹并执行此操作
$ git config user.name "Working Hard"
$ git config user.email "work@company.com"
Run Code Online (Sandbox Code Playgroud)
只是看看这能检查“ .git / config”的内容
进入该文件夹,其中用户个人将工作和执行这个
$ git config user.name "Personal Account"
$ git config user.email "pers@email.com"
Run Code Online (Sandbox Code Playgroud)
只是看看这能检查“ .git / config”的内容
完成所有这些之后,您只需在这两个文件夹之间切换就可以提交您的个人和工作代码
视窗环境
此外Git Extensions --> Settings --> Global Settings,如果您在系统中安装了它,则可以从 修改它。
右键单击 Windows 环境中的文件夹/目录以访问这些设置。

GIT_AUTHOR_EMAIL +本地 .bashrc
.bashrc_local:请勿跟踪此文件,仅将其放在您的工作计算机上:
export GIT_AUTHOR_EMAIL='me@work.com'
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
Run Code Online (Sandbox Code Playgroud)
.bashrc:跟踪此文件,使其在工作计算机和家用计算机上都相同:
F="$HOME/.bashrc_local"
if [ -r "$F" ]; then
. "$F"
fi
Run Code Online (Sandbox Code Playgroud)
我正在使用https://github.com/technicalpickles/homesick同步我的点文件。
如果只有gitconfig会接受环境变量:git config中的Shell变量扩展
| 归档时间: |
|
| 查看次数: |
198753 次 |
| 最近记录: |