我使用两个不同的Git电子邮件,一个用于工作,另一个用于公共项目。最初我认为我可以在存放所有公共仓库的文件夹中创建一个单独的.gitconfig,并在其中使用不同的电子邮件地址,这样Git就会遵循它,但不幸的是似乎并不起作用。有什么简单的设置方式吗?我想避免在每个公共仓库中具体更改电子邮件。
我使用两个不同的Git电子邮件,一个用于工作,另一个用于公共项目。最初我认为我可以在存放所有公共仓库的文件夹中创建一个单独的.gitconfig,并在其中使用不同的电子邮件地址,这样Git就会遵循它,但不幸的是似乎并不起作用。有什么简单的设置方式吗?我想避免在每个公共仓库中具体更改电子邮件。
**
。 这样出错的机会较小,而且我更喜欢少些神奇操作。 - Tim Harper如其他回答中所述,您无法为每个目录单独设置凭据。但是,git允许您对每个存储库进行这样的设置。
# Require setting user.name and email per-repo
$ git config --global user.useConfigOnly true
这样,当您第一次提交时,会出现一个错误提示您提供姓名和电子邮件。在您的存储库文件夹中添加您的凭据一次,然后从那时起,您将使用此身份验证进行提交:
$ cd path/to/repo
$ git config user.name My Name
$ git config user.email mail@example.com
我有完全相同的问题。作为临时解决方案,我在.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
.gitemail
文件:
~/work/.gitemail
~/github/.gitemail
user.email
,其他所有内容都在~/.gitconfig
中集中管理。这是一种解决方案,但并不理想。我已经转换到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
还有在您的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"
}
~/.zshrc
,我需要有一个单独的文件吗? - avi建立一个集中式机制来创建仓库,例如在您的路径中的一些脚本等,通过以下方式实现:
repo create -t public -n name
或类似的东西。
repo 命令(只是一个示例,与 Android 的命令无关)会在必要的位置为您创建 repo,并读取配置文件并根据类型(公共或私有等)为该 repo 设置凭据(在该 repo 的 .git/config 文件中)。
# 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;
}
# Look for closest .gitconfig file in parent directories
# This file will be used as main .gitconfig file.
function __recursive_gitconfig_git {
gitconfig_file=$(__recursive_gitconfig_closest)
if [ "$gitconfig_file" != '' ]; then
home="$(dirname $gitconfig_file)/"
HOME=$home /usr/bin/git "$@"
else
/usr/bin/git "$@"
fi
}
# Look for closest .gitconfig file in parents directories
function __recursive_gitconfig_closest {
slashes=${PWD//[^\/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/.gitconfig" && echo "$directory/.gitconfig" && return
directory="$directory/.."
done
}
alias git='__recursive_gitconfig_git'
这让我能够根据我正在使用的存储库使用特定的 .gitconfig。
希望这能帮助到你们中的一些人