如何告诉 git 对给定项目使用正确的身份(名称和电子邮件)?
我将我的个人笔记本电脑用于工作和个人项目,我想使用我的工作电子邮件地址进行工作提交 (gitolite),并使用我的个人电子邮件地址进行其余工作 (github)。
我读到了以下解决方案,这些解决方案都是全局或临时的:
- git config --global user.email "[电子邮件受保护]"
git config user.email "[电子邮件受保护]"
- git commit --author "鲍勃 <[电子邮件受保护]>"
- 设置
GIT_AUTHOR_EMAIL
、GIT_COMMITTER_EMAIL
之一或EMAIL
环境变量
一种解决方案是手动运行一个 shell 函数,将我的环境设置为工作或个人,但我很确定我经常会忘记切换到正确的身份,从而导致在错误的身份。
有没有办法将某个存储库、项目名称等绑定到身份(姓名、电子邮件)?人们做什么?
I use my personal laptop for both work and personal projects and I would like to use my work email address for my commits at work (gitolite) and my personal email address for the rest (github).
I read about the following solutions which are all either global or temporary:
git config --global user.email "[email protected]"
git config user.email "[email protected]"
git commit --author "Bob <[email protected]>"
- setting one of the
GIT_AUTHOR_EMAIL
,GIT_COMMITTER_EMAIL
orEMAIL
environment variables
One solution is to run manually a shell function that sets my environment to work or personal, but I am pretty sure that I will often forget to switch to the correct identity resulting in committing under the wrong identity.
Is there a way of binding a certain repository, project name, etc. to an identity (name, email)? What do people do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
git config user.email "[电子邮件受保护]"
在存储库中执行此操作将在该存储库上设置配置,而不是全局配置。
看来这正是你所追求的,除非我误读了你。
git config user.email "[email protected]"
Doing that one inside a repo will set the configuration on THAT repo, and not globally.
Seems like that's pretty much what you're after, unless I'm misreading you.
您需要使用下面的local set命令:
local set
local get
本地配置文件位于项目目录中:
.git /配置。
全局设置
全局获取
主目录中的全局配置文件:
~/.gitconfig
。请记住引用空格等,例如:'FirstName LastName'
You need to use the local set command below:
local set
local get
The local config file is in the project directory:
.git/config
.global set
global get
The global config file in in your home directory:
~/.gitconfig
.Remember to quote blanks, etc, for example: 'FirstName LastName'
编辑“.git”文件夹中的配置文件,以维护不同的用户名和电子邮件,具体取决于存储库
下面的命令显示您为此存储库设置的用户名和电子邮件。
示例:对于我的配置文件,位于 D:\workspace\eclipse\ipchat\.git\config
这里 ipchat 是我的存储库名称
Edit the config file with in ".git" folder to maintain the different username and email depends upon the repository
This below command show you which username and email set for this repository.
Example: for mine that config file in D:\workspace\eclipse\ipchat\.git\config
Here ipchat is my repo name
如果您使用 git config user.email "[email protected] “ 它将绑定到您当前所在的项目。
这就是我为我的项目所做的。当我克隆/初始化存储库时,我设置了适当的身份。它并不是万无一失的(如果你在弄清楚之前忘记并推动了你就被淘汰了),但它几乎是你在无法说 git config --global user.email 'ILLEGAL_VALUE' 的情况下所能得到的最好的结果。
实际上,你可以设置一个非法值。设置您的 git config --global user.name $(perl -e 'print "x"x968;')
然后,如果您忘记设置非全局值,您将收到一条错误消息。
[编辑]
在另一个系统上,我必须将 x 的数量增加到 968,才能使其失败并显示“致命:不可能长的个人标识符”。相同版本的 git。奇怪的。
If you use
git config user.email "[email protected]"
it will be bound to the current project you are in.That is what I do for my projects. I set the appropriate identity when I clone/init the repo. It is not fool-proof (if you forget and push before you figure it out you are hosed) but it is about as good as you can get without the ability to say
git config --global user.email 'ILLEGAL_VALUE'
Actually, you can make an illegal value. Set your
git config --global user.name $(perl -e 'print "x"x968;')
Then if you forget to set your non-global values you will get an error message.
[EDIT]
On a different system I had to increase the number of x to 968 to get it to fail with "fatal: Impossibly long personal identifier". Same version of git. Strange.
从 Git 2.13 开始,您可以在 gitconfig 中使用
includeIf
来根据运行 git 命令的存储库路径包含具有不同配置的文件。由于 Ubuntu 18.04 附带了一个足够新的 Git,我一直很高兴地在我的
~/.gitconfig
中使用它。https://motowilliams.com/conditional-includes-for-git-config#disqus_thread
要使用 Git 2.13,您需要添加 PPA(早于 18.04/Debian 的 Ubuntu)或下载二进制文件并安装(Windows/其他 Linux)。
As of Git 2.13 you can use an
includeIf
in your gitconfig to include a file with a different configuration based on the path of the repository where you are running your git commands.Since a new enough Git comes with Ubuntu 18.04 I've been using this in my
~/.gitconfig
quite happily.https://motowilliams.com/conditional-includes-for-git-config#disqus_thread
To use Git 2.13 you will either need to add a PPA (Ubuntu older than 18.04/Debian) or download the binaries and install (Windows/other Linux).
如果您不使用
--global
参数,它将仅为当前项目设置变量。If you don't use the
--global
parameter it will set the variables for the current project only.我非常喜欢 Micah Henning 在他的文章中的方式(请参阅 设置 Git 身份)关于这个主题。事实上,他将身份应用于创建/克隆的每个存储库,这是一个不要忘记每次都进行设置的好方法。
基本 git 配置
在 git 中取消当前用户配置:
在每个新的本地存储库上强制进行身份配置:
为
identity
命令创建 Git 别名,我们稍后将使用:身份创建
使用 GPG 创建身份(使用
gpg
或gpg2
取决于您系统上的内容)。对您要使用的每个身份重复后续步骤。复制公钥块并将其作为 GPG 密钥添加到您的 GitHub/GitProviderOfChoice 设置中。
将身份添加到 Git 配置。还要对您要添加的每个身份重复此操作:
设置存储库的标识
如果新存储库没有关联的标识,则提交时会出现错误,提醒您设置它。
指定您想要在新存储库上使用的身份:
您现在已准备好使用 gitlab 身份进行提交。
I very like the way of Micah Henning in his article (see Setting Up Git Identities) on this subject. The fact that he apply and force the identity to each repository created/cloned is a good way not to forget to set this up each time.
Basic git configuration
Unset current user config in git:
Force identity configuration on each new local repository:
Create Git alias for
identity
command, we will use later:Identities creation
Create an identity with GPG (use
gpg
orgpg2
depending on what you got on your system). Repeat next steps for each identities you want to use.Copy the public key block and add it to your GitHub/GitProviderOfChoice settings as a GPG key.
Add identity to Git config. Also repeat this for each identity you want to add:
Setup identity for a repository
If a new repo has no identity associated, an error will appear on commit, reminding you to set it.
Specify the identity you want on a new repository:
You're now ready to commit with the gitlab identity.
这正是我的问题。我编写了一个钩子脚本,如果您有任何 github 远程并且未定义本地用户名,它会警告您。
设置方法如下:
创建一个目录来保存全局挂钩
mkdir -p ~/.git-templates/hooks
当您运行 git init 时,告诉 git 将
~/.git-templates
中的所有内容复制到每个项目的.git
目录中或者克隆git config --global init.templatedir '~/.git-templates'
并且现在将以下行复制到
~/.git-templates/hooks/pre-commit
并使文件可执行(不要忘记这一点,否则 git 不会执行它!)如果您使用其他主机对于您的私人存储库,您必须替换
github.com
根据您的需要。现在,每次执行 git init 或 git clone 时,git 都会将此脚本复制到存储库并在完成任何提交之前执行它。如果您尚未设置本地用户名,它将输出警告并且不会让您提交。
That was exactly my problem. I have written a hook script which warns you if you have any github remote and not defined a local username.
Here's how you set it up:
Create a directory to hold the global hook
mkdir -p ~/.git-templates/hooks
Tell git to copy everything in
~/.git-templates
to your per-project.git
directory when you run git init or clonegit config --global init.templatedir '~/.git-templates'
And now copy the following lines to
~/.git-templates/hooks/pre-commit
and make the file executable (don't forget this otherwise git won't execute it!)If you use other hosts for your private repositories you have to replace
github.com
according to your needs.Now every time you do a
git init
orgit clone
git will copy this script to the repository and executes it before any commit is done. If you have not set a local username it will output a warning and won't let you commit.git config --global user.name "你的名字"
git config --global user.email "你的电子邮件"
git config --global user.name "your name"
git config --global user.email "your email"