如何告诉git为给定项目使用正确的标识(名称和电子邮件)?
我在工作和个人项目中都使用我的个人笔记本电脑,我想在工作中使用我的工作电子邮件地址(gitolite),在其他项目中使用我的个人电子邮件地址(github) 我了解到以下解决方案,这些解决方案都是全局性的或临时性的:如何告诉git为给定项目使用正确的标识(名称和电子邮件)?,git,github,gitolite,git-config,Git,Github,Gitolite,Git Config,我在工作和个人项目中都使用我的个人笔记本电脑,我想在工作中使用我的工作电子邮件地址(gitolite),在其他项目中使用我的个人电子邮件地址(github) 我了解到以下解决方案,这些解决方案都是全局性的或临时性的: git配置--global user.email”bob@example.com“ git config user.email”bob@example.com“ git提交——编写“Bob” 设置一个GIT\u作者\u电子邮件,GIT\u提交者\u电子邮件或电子邮件环境变量 一
git配置--global user.email”bob@example.com“
git config user.email”bob@example.com“
git提交——编写“Bob”
- 设置一个
,GIT\u作者\u电子邮件
或GIT\u提交者\u电子邮件
环境变量电子邮件
是否有办法将某个存储库、项目名称等绑定到一个标识(名称、电子邮件)?人们会做什么?如果使用
git config user.email“foo@example.com“
它将绑定到您当前所在的项目
这就是我为我的项目所做的。我在克隆/初始化repo时设置了适当的标识。它不是傻瓜式的(如果你忘记了,在你弄清楚之前推了推,你就被套住了),但它是你在没有说git config--global user.email“非法”的情况下所能得到的最好的东西。
实际上,您可以生成非法值。设置您的git配置--global user.name$(perl-e'print“x”x968;)
然后,如果您忘记设置非全局值,您将收到一条错误消息
[编辑]
在另一个系统上,我不得不将x的数量增加到968,以使其以“致命的:不可能长的个人标识符”失败。相同版本的git。奇怪。如果不使用
--global
参数,它将仅为当前项目设置变量。git config user.email”bob@example.com“
在回购协议中执行该操作将在该回购协议上设置配置,而不是全局设置
看起来这就是你想要的,除非我误解了你。在“.git”文件夹中编辑配置文件,以维护不同的用户名和电子邮件,具体取决于存储库
- 转到您的存储库
- 显示隐藏的文件并转到“.git”文件夹
- 找到“配置”文件
- 在EOF处添加以下行
这里ipchat是我的回购名称您需要使用下面的本地设置命令: 本地集
git config user.email mahmoud@company.ccc
git config user.name 'Mahmoud Zalt'
git config --get user.email
git config --get user.name
git config --global user.email mahmoud@zalt.me
git config --global user.name 'Mahmoud Zalt'
git config --global --get user.email
git config --global --get user.name
本地获取
git config user.email mahmoud@company.ccc
git config user.name 'Mahmoud Zalt'
git config --get user.email
git config --get user.name
git config --global user.email mahmoud@zalt.me
git config --global user.name 'Mahmoud Zalt'
git config --global --get user.email
git config --global --get user.name
本地配置文件位于项目目录中:.git/config
全局集
git config user.email mahmoud@company.ccc
git config user.name 'Mahmoud Zalt'
git config --get user.email
git config --get user.name
git config --global user.email mahmoud@zalt.me
git config --global user.name 'Mahmoud Zalt'
git config --global --get user.email
git config --global --get user.name
全球get
git config user.email mahmoud@company.ccc
git config user.name 'Mahmoud Zalt'
git config --get user.email
git config --get user.name
git config --global user.email mahmoud@zalt.me
git config --global user.name 'Mahmoud Zalt'
git config --global --get user.email
git config --global --get user.name
主目录中的全局配置文件:~/.gitconfig
记住引用空格等,例如:“FirstName LastName”从Git 2.13开始,您可以在gitconfig中使用
includeIf
,根据运行Git命令的存储库的路径,包含具有不同配置的文件
自从Ubuntu18.04附带了一个足够新的Git之后,我一直很高兴地在我的~/.gitconfig
中使用它
[include]
path = ~/.gitconfig.alias # I like to keep global aliases separate
path = ~/.gitconfig.defaultusername # can maybe leave values unset/empty to get warned if a below path didn't match
# If using multiple identities can use per path user/email
# The trailing / is VERY important, git won't apply the config to subdirectories without it
[includeIf "gitdir:~/projects/azure/"]
path = ~/.gitconfig.azure # user.name and user.email for Azure
[includeIf "gitdir:~/projects/gitlab/"]
path = ~/.gitconfig.gitlab # user.name and user.email for GitLab
[includeIf "gitdir:~/projects/foss/"]
path = ~/.gitconfig.github # user.name and user.email for GitHub
要使用Git2.13,您需要添加一个PPA(Ubuntu早于18.04/Debian),或者下载二进制文件并安装(Windows/OtherLinux)
一种解决方案是手动运行一个shell函数,将我的环境设置为“工作”或“个人”,但我非常确定,我经常会忘记切换到正确的标识,从而导致使用错误的标识进行提交
那正是我的问题。我编写了一个钩子脚本,如果您有任何github remote并且没有定义本地用户名,它会警告您
下面是您如何设置它的:
mkdir-p~/.git模板/挂钩
~/.git模板中的所有内容复制到每个项目.git
目录中
git config--global init.templatedir'~/.git templates'
~/.git templates/hooks/pre-commit
并使文件可执行(不要忘记这一点,否则git将不会执行它!)#/bin/bash
红色='\033[0;31m'#红色
NC='\033[0m'#无颜色
GITHUB_REMOTE=$(git REMOTE-v | grep GITHUB.com)
LOCAL_USERNAME=$(git config--LOCAL user.name)
如果[-n“$GITHUB\u REMOTE”]&&&[-z“$LOCAL\u USERNAME”];则
printf“\n${RED}注意:至少配置了一个Github远程存储库,但没有本地用户名。”
printf“请定义与您的Github帐户匹配的本地用户名。${NC}[预提交挂钩]\n\n”
出口1
fi
如果您的私有存储库使用其他主机,则必须根据需要更换github.com
现在,每次您执行
git init
或git clone
时,git都会将此脚本复制到存储库并在任何提交之前执行。如果您没有设置本地用户名,它将输出警告,并且不允许您提交。我非常喜欢Micah Henning在文章中的方式(请参阅)在这个问题上,他对创建/克隆的每个存储库应用并强制使用标识,这是一个很好的方法,每次都不要忘记设置它
基本git配置
在git中取消设置当前用户配置:
$ git config --global --unset user.name
$ git config --global --unset user.email
$ git config --global --unset user.signingkey
在每个新的本地存储库上强制进行标识配置:
$ git config --global user.useConfigOnly true
$ git identity gitlab
为identity
命令创建Git别名,我们将