如何告诉 git 为给定项目使用正确的身份(姓名和电子邮件)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6116548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to tell git to use the correct identity (name and email) for a given project?
提问by Martin Jambon
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).
我将我的个人笔记本电脑用于工作和个人项目,我想将我的工作电子邮件地址用于我的工作提交 (gitolite) 和我的个人电子邮件地址 (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
git config --global user.email "[email protected]"
git config user.email "[email protected]"
git commit --author "Bob <[email protected]>"
- 设置
GIT_AUTHOR_EMAIL
,GIT_COMMITTER_EMAIL
或EMAIL
环境变量之一
One solution is to run manually a shell function that sets my environment to workor personal, but I am pretty sure that I will often forget to switch to the correct identity resulting in committing under the wrong identity.
一种解决方案是手动运行一个 shell 函数,将我的环境设置为work或personal,但我很确定我经常忘记切换到正确的身份,从而导致以错误的身份提交。
Is there a way of binding a certain repository, project name, etc. to an identity (name, email)? What do people do?
有没有办法将某个存储库、项目名称等绑定到身份(姓名、电子邮件)?人们做什么?
回答by Dan Ray
git config user.email "[email protected]"
git config user.email "[email protected]"
Doing that one inside a repo will set the configuration on THAT repo, and not globally.
在 repo 中执行此操作将在该 repo 上设置配置,而不是全局设置。
Seems like that's pretty much what you're after, unless I'm misreading you.
除非我误读了您,否则这似乎就是您所追求的。
回答by Mahmoud Zalt
You need to use the local setcommand below:
您需要使用下面的本地设置命令:
local set
本地集
git config user.email [email protected]
git config user.name 'Mahmoud Zalt'
local get
本地获取
git config --get user.email
git config --get user.name
The local config file is in the project directory: .git/config
.
本地配置文件位于项目目录中:.git/config
.
global set
全局集
git config --global user.email [email protected]
git config --global user.name 'Mahmoud Zalt'
global get
全局获取
git config --global --get user.email
git config --global --get user.name
The global config file in in your home directory: ~/.gitconfig
.
主目录中的全局配置文件:~/.gitconfig
.
Remember to quote blanks, etc, for example: 'FirstName LastName'
记得引用空格等,例如:'FirstName LastName'
回答by arulraj.net
Edit the config file with in ".git" folder to maintain the different username and email depends upon the repository
使用“.git”文件夹编辑配置文件以维护不同的用户名和电子邮件取决于存储库
- Go to Your repository
- Show the hidden files and go to ".git" folder
- Find the "config" file
- Add the below lines at EOF
- 转到您的存储库
- 显示隐藏文件并转到“.git”文件夹
- 找到“配置”文件
- 在 EOF 处添加以下几行
[user]
name = Bob
email = [email protected]
[用户]
姓名 = 鲍勃
电子邮件 = [email protected]
This below command show you which username and email set for this repository.
下面的命令显示您为此存储库设置的用户名和电子邮件。
git config --get user.name
git config --get user.email
git config --get 用户名
git config --get user.email
Example: for mine that config file in D:\workspace\eclipse\ipchat\.git\config
示例:对于我的 D:\workspace\eclipse\ipchat\.git\config 中的配置文件
Here ipchat is my repo name
这里 ipchat 是我的仓库名称
回答by Seth Robertson
If you use git config user.email "[email protected]"
it will be bound to the current project you are in.
如果您使用git config user.email "[email protected]"
它,它将绑定到您所在的当前项目。
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'
这就是我为我的项目所做的。我在克隆/初始化 repo 时设置了适当的身份。这不是万无一失的(如果你在弄清楚之前忘记并推动,你会被冲洗掉)但它与你无法说出来的一样好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;')
实际上,您可以生成非法值。设置你的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.
[编辑] 在不同的系统上,我不得不将 x 的数量增加到 968 以使其失败并显示“致命:不可能长的个人标识符”。相同版本的 git。奇怪的。
回答by ismail
If you don't use the --global
parameter it will set the variables for the current project only.
如果您不使用该--global
参数,它将仅为当前项目设置变量。
回答by dragon788
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.
从 Git 2.13 开始,您可以includeIf
在 gitconfig 中使用,根据运行 git 命令的存储库路径包含具有不同配置的文件。
Since a new enough Git comes with Ubuntu 18.04 I've been using this in my ~/.gitconfig
quite happily.
自从 Ubuntu 18.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
https://motowilliams.com/conditional-includes-for-git-config#disqus_thread
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).
要使用 Git 2.13,您需要添加 PPA(Ubuntu 18.04/Debian 之前的版本)或下载二进制文件并安装(Windows/其他 Linux)。
回答by naitsirch
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.
一种解决方案是手动运行一个 shell 函数,将我的环境设置为工作或个人环境,但我很确定我经常会忘记切换到正确的身份,从而导致以错误的身份提交。
That was exactly my problem. I have written a hook script which warns you if you have any github remote and notdefined a local username.
那正是我的问题。我写了一个钩子脚本,如果你有任何 github 远程并且没有定义本地用户名,它会警告你。
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 initor 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!)
创建一个目录来保存全局钩子
mkdir -p ~/.git-templates/hooks
当您运行git init或clone时,告诉 git 将所有内容复制
~/.git-templates
到您的每个项目.git
目录中git config --global init.templatedir '~/.git-templates'
现在复制以下几行
~/.git-templates/hooks/pre-commit
并使文件可执行(不要忘记这一点,否则 git 不会执行它!)
#!/bin/bash
RED='3[0;31m' # red color
NC='3[0m' # no color
GITHUB_REMOTE=$(git remote -v | grep github.com)
LOCAL_USERNAME=$(git config --local user.name)
if [ -n "$GITHUB_REMOTE" ] && [ -z "$LOCAL_USERNAME" ]; then
printf "\n${RED}ATTENTION: At least one Github remote repository is configured, but no local username. "
printf "Please define a local username that matches your Github account.${NC} [pre-commit hook]\n\n"
exit 1
fi
If you use other hosts for your private repositories you have to replace github.com
according to your needs.
如果您将其他主机用于您的私有存储库,则必须github.com
根据您的需要进行更换。
Now every time you do a git init
or git 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.
现在每次执行 agit init
或git clone
git 都会将此脚本复制到存储库并在完成任何提交之前执行它。如果你没有设置本地用户名,它会输出一个警告并且不会让你提交。