Sua*_*uan 27 git email git-config
我使用两个不同的git电子邮件,一个用于工作,一个用于公共项目.最初我以为我可以在我所有的公共存储库所在的文件夹中创建一个单独的.gitconfig和不同的电子邮件,而git会尊重它,但唉似乎不起作用.什么是轻松设置类似内容的最佳方式?我想避免在每个公共回购中专门更改电子邮件.
Chr*_*son 44
自 git 2.13以来,最好的方法是使用Conditional includes。
一个例子(从这里的答案复制):
全局配置 ~/.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)
我有完全一样的问题.作为一个临时解决方案,我在我的.bashrc
:
alias git='GIT_AUTHOR_EMAIL=$(
p=$(pwd)
while [[ $p != "$HOME" ]]; do
[ -e $p/.gitemail ] && cat $p/.gitemail && break
p=$(dirname $p)
done) GIT_COMMITTER_EMAIL=$(
p=$(pwd)
while [[ $p != "$HOME" ]]; do
[ -e $p/.gitemail ] && cat $p/.gitemail && break
p=$(dirname $p)
done) /usr/bin/git'
alias g=git
Run Code Online (Sandbox Code Playgroud)
这样我.gitemail
在父目录中有两个不同的文件:
~/work/.gitemail
~/github/.gitemail
请注意,我只是这样切换user.email
,其他一切都集中在~/.gitconfig
.这是一个解决方案,但并不是很好.
希望StackOverflow上有人有更好的想法......
切换到ZSH后,这是我使用在目录更改时触发的"配置文件"修改了问题的解决方案.这个解决方案的好处是它可以用于其他设置.
将此弹出到您的zsh配置中:
#
# Thanks to: Michael Prokop.
# More documentation:
# http://git.grml.org/?p=grml-etc-core.git;f=etc/zsh/zshrc;hb=HEAD#l1120
#
CHPWD_PROFILE='default'
function chpwd_profiles() {
local -x profile
zstyle -s ":chpwd:profiles:${PWD}" profile profile || profile='default'
if (( ${+functions[chpwd_profile_$profile]} )) ; then
chpwd_profile_${profile}
fi
CHPWD_PROFILE="${profile}"
return 0
}
chpwd_functions=( ${chpwd_functions} chpwd_profiles )
chpwd_profile_default # run DEFAULT profile automatically
Run Code Online (Sandbox Code Playgroud)
然后在你的zsh git自定义中的其他地方:
zstyle ':chpwd:profiles:/home/user/work(|/|/*)' profile work
zstyle ':chpwd:profiles:/home/user/fun(|/|/*)' profile fun
# configuration for profile 'default':
chpwd_profile_default()
{
[[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
print "chpwd(): Switching to profile: default"
export GIT_AUTHOR_EMAIL="default@example.com"
export GIT_COMMITTER_EMAIL="default@example.com"
}
# configuration for profile 'fun':
chpwd_profile_fun()
{
[[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
print "chpwd(): Switching to profile: $profile"
export GIT_AUTHOR_EMAIL="fun@example.com"
export GIT_COMMITTER_EMAIL="fun@example.com"
}
# configuration for profile 'work':
chpwd_profile_work()
{
[[ ${profile} == ${CHPWD_PROFILE} ]] && return 1
print "chpwd(): Switching to profile: $profile"
export GIT_AUTHOR_EMAIL="work@example.com"
export GIT_COMMITTER_EMAIL="work@example.com"
}
Run Code Online (Sandbox Code Playgroud)
真正的答案是它不可能.
但是,感谢@pithyless,并且因为我已经使用自定义'c'函数来切换目录然后是auto ls
,这就是我现在正在做的事情:
# cd + ls, and change git email when in personal projects folder
function c {
if [[ "`abspath $1`" == *"$HOME/Projects"* ]]; then
export GIT_AUTHOR_EMAIL="personal@gmail.com"
else
export GIT_AUTHOR_EMAIL="me@work.com"
fi
cd "${@:-$HOME}" && ls;
}
Run Code Online (Sandbox Code Playgroud)
正如其他答案所述,您无法真正为每个目录设置凭据.但是git允许你在每个存储库的基础上这样做.
# Require setting user.name and email per-repo
$ git config --global user.useConfigOnly true
Run Code Online (Sandbox Code Playgroud)
这样,在您第一次提交时,您将收到一个错误,要求您提供姓名和电子邮件.在您的repo文件夹中将您的凭据添加到您的仓库一次,然后您将使用此身份提交:
$ cd path/to/repo
$ git config user.name My Name
$ git config user.email mail@example.com
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3563 次 |
最近记录: |