如何告诉 git 对给定项目使用正确的身份(名称和电子邮件)?

发布于 2024-11-09 15:45:03 字数 891 浏览 0 评论 0原文

我将我的个人笔记本电脑用于工作和个人项目,我想使用我的工作电子邮件地址进行工作提交 (gitolite),并使用我的个人电子邮件地址进行其余工作 (github)。

我读到了以下解决方案,这些解决方案都是全局或临时的:

一种解决方案是手动运行一个 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:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

|煩躁 2024-11-16 15:45:03

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.

染柒℉ 2024-11-16 15:45:03

您需要使用下面的local set命令:

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

本地配置文件位于项目目录中:.git /配置。

全局设置

git config --global user.email [email protected]
git config --global user.name 'Mahmoud Zalt'

全局获取

git config --global --get user.email
git config --global --get user.name

主目录中的全局配置文件:~/.gitconfig

请记住引用空格等,例如:'FirstName LastName'

You need to use the local set command 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.

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.

Remember to quote blanks, etc, for example: 'FirstName LastName'

空城缀染半城烟沙 2024-11-16 15:45:03

编辑“.git”文件夹中的配置文件,以维护不同的用户名和电子邮件,具体取决于存储库

  • 转到您的存储库
  • 显示隐藏文件并转到“.git”文件夹
  • 查找“config”文件
  • 在 EOF 处添加以下行

[用户]

姓名=鲍勃

电子邮件 = [电子邮件受保护]

下面的命令显示您为此存储库设置的用户名和电子邮件。

git config --获取用户名

git config --获取用户.email

示例:对于我的配置文件,位于 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

  • Go to Your repository
  • Show the hidden files and go to ".git" folder
  • Find the "config" file
  • Add the below lines at EOF

[user]

name = Bob

email = [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

Example: for mine that config file in D:\workspace\eclipse\ipchat\.git\config

Here ipchat is my repo name

淡看悲欢离合 2024-11-16 15:45:03

如果您使用 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.

自演自醉 2024-11-16 15:45:03

从 Git 2.13 开始,您可以在 gitconfig 中使用 includeIf 来根据运行 git 命令的存储库路径包含具有不同配置的文件。

由于 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

要使用 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.

[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

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).

撕心裂肺的伤痛 2024-11-16 15:45:03

如果您不使用 --global 参数,它将仅为当前项目设置变量。

If you don't use the --global parameter it will set the variables for the current project only.

溺渁∝ 2024-11-16 15:45:03

我非常喜欢 Micah Henning 在他的文章中的方式(请参阅 设置 Git 身份)关于这个主题。事实上,他将身份应用于创建/克隆的每个存储库,这是一个不要忘记每次都进行设置的好方法。

基本 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

identity 命令创建 Git 别名,我们稍后将使用:

$ git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; git config user.signingkey "$(git config user.$1.signingkey)"; :'

身份创建

使用 GPG 创建身份(使用 gpggpg2 取决于您系统上的内容)。对您要使用的每个身份重复后续步骤。

注意:这里的[keyid]是创建的密钥的标识符
示例如下:

<前><代码>秒 rsa4096/8A5C011E4CE081A5 2020-06-09 [SC] [过期:2021-06-09]
CCC470AE787C057557F421488C4C951E4CE081A5
uid [最终] 您的姓名
ssb rsa4096/1EA965889861C1C0 2020-06-09 [E] [过期: 2021-06-09]

sec rsa4096/之后的8A5C011E4CE081A5部分是密钥的标识符

$ gpg --full-gen-key
$ gpg --list-secret-keys --keyid-format LONG <youremail@domain>
$ gpg --armor --export [keyid]

复制公钥块并将其作为 GPG 密钥添加到您的 GitHub/GitProviderOfChoice 设置中。

将身份添加到 Git 配置。还要对您要添加的每个身份重复此操作:

注意:这里我使用 gitlab命名我的身份,但从你的问题来看,它可以是任何东西,例如:gitolitegithub工作

$ git config --global user.gitlab.name "Your Name"
$ git config --global user.gitlab.email "youremail@domain"
$ git config --global user.gitlab.signingkey [keyid]

设置存储库的标识

如果新存储库没有关联的标识,则提交时会出现错误,提醒您设置它。

*** Please tell me who you are.

## parts of message skipped ##

fatal: no email was given and auto-detection is disabled

指定您想要在新存储库上使用的身份:

$ git identity gitlab

您现在已准备好使用 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:

$ git config --global --unset user.name
$ git config --global --unset user.email
$ git config --global --unset user.signingkey

Force identity configuration on each new local repository:

$ git config --global user.useConfigOnly true

Create Git alias for identity command, we will use later:

$ git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; git config user.signingkey "$(git config user.$1.signingkey)"; :'

Identities creation

Create an identity with GPG (use gpg or gpg2 depending on what you got on your system). Repeat next steps for each identities you want to use.

Note: [keyid] here is the identifier of created secret key.
Example here:

sec   rsa4096/8A5C011E4CE081A5 2020-06-09 [SC] [expires: 2021-06-09]
      CCC470AE787C057557F421488C4C951E4CE081A5
uid                 [ultimate] Your Name <youremail@domain>
ssb   rsa4096/1EA965889861C1C0 2020-06-09 [E] [expires: 2021-06-09]

The 8A5C011E4CE081A5 part after sec rsa4096/ is the identifier of key.

$ gpg --full-gen-key
$ gpg --list-secret-keys --keyid-format LONG <youremail@domain>
$ gpg --armor --export [keyid]

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:

Note: here I use gitlab to name my identity, but from your question it can be anything, ex: gitolite or github, work, etc.

$ git config --global user.gitlab.name "Your Name"
$ git config --global user.gitlab.email "youremail@domain"
$ git config --global user.gitlab.signingkey [keyid]

Setup identity for a repository

If a new repo has no identity associated, an error will appear on commit, reminding you to set it.

*** Please tell me who you are.

## parts of message skipped ##

fatal: no email was given and auto-detection is disabled

Specify the identity you want on a new repository:

$ git identity gitlab

You're now ready to commit with the gitlab identity.

不疑不惑不回忆 2024-11-16 15:45:03

一种解决方案是手动运行 shell 函数,将我的环境设置为工作或个人环境,但我很确定我经常会忘记切换到正确的身份,从而导致使用错误的身份进行提交。

这正是我的问题。我编写了一个钩子脚本,如果您有任何 github 远程并且定义本地用户名,它会警告您。

设置方法如下:

  1. 创建一个目录来保存全局挂钩

    mkdir -p ~/.git-templates/hooks

  2. 当您运行 git init 时,告诉 git 将 ~/.git-templates 中的所有内容复制到每个项目的 .git 目录中或者克隆

    git config --global init.templatedir '~/.git-templates'

  3. 并且现在将以下行复制到 ~/.git-templates/hooks/pre-commit 并使文件可执行(不要忘记这一点,否则 git 不会执行它!)

#!/bin/bash

RED='\033[0;31m' # red color
NC='\033[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

如果您使用其他主机对于您的私人存储库,您必须替换github.com 根据您的需要。

现在,每次执行 git init 或 git clone 时,git 都会将此脚本复制到存储库并在完成任何提交之前执行它。如果您尚未设置本地用户名,它将输出警告并且不会让您提交。

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.

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:

  1. Create a directory to hold the global hook

    mkdir -p ~/.git-templates/hooks

  2. Tell git to copy everything in ~/.git-templates to your per-project .git directory when you run git init or clone

    git config --global init.templatedir '~/.git-templates'

  3. 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!)

#!/bin/bash

RED='\033[0;31m' # red color
NC='\033[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.

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.

蓝眼泪 2024-11-16 15:45:03

git config --global user.name "你的名字"

git config --global user.email "你的电子邮件"

git config --global user.name "your name"

git config --global user.email "your email"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文