我能够在.gitconfig中为本身指定多个用户吗?

~/.gitconfig ,我在[user]个人我的电子邮件地址,由于这是我要用于Github存储库的地址。 html

可是,我最近也开始使用git进行工做。 我公司的git repo容许我提交,可是当它发布新变动集的公告时,它说它们来自匿名用户,由于它没法识别个人.gitconfig的电子邮件地址-至少,这是个人理论。 git

是否能够在.gitconfig指定多个[user]定义? 仍是有其余方法能够覆盖某个目录的默认.gitconfig ? 就我而言,我在~/worksrc/签出全部工做代码-有没有一种方法能够仅为该目录(及其子目录)指定.gitconfiggithub


#1楼

或者,您能够在本地.git/config文件中添加如下信息 bash

[user]  
    name = Your Name
    email = your.email@gmail.com

#2楼

Orr Sella的博客文章中得到一些启发后,我编写了一个预提交钩子(位于~/.git/templates/hooks ),该~/.git/templates/hooks将根据本地存储库./.git/config的信息设置特定的用户名和电子邮件地址。 ./.git/configide

您必须将模板目录的路径放入~/.gitconfigpost

[init]
    templatedir = ~/.git/templates

而后,每一个git initgit clone将选择该钩子,并在下一次git commit期间应用用户数据。 若是要将钩子应用于已经存在的存储库,则只需在存储库中运行git init便可将其从新初始化。 ui

这是我想出的钩子(仍然须要打磨-欢迎提出建议)。 另存为 this

~/.git/templates/hooks/pre_commit

要么 url

~/.git/templates/hooks/post-checkout

并确保它是可执行的: chmod +x ./post-checkout || chmod +x ./pre_commit chmod +x ./post-checkout || chmod +x ./pre_commit spa

#!/usr/bin/env bash

# -------- USER CONFIG
# Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
git_remotes[0]="Github"
git_remotes[1]="Gitlab"

# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"

local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"

local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"


# -------- FUNCTIONS
setIdentity()
{
    local current_id local_id

    current_id[0]="$(git config --get --local user.name)"
    current_id[1]="$(git config --get --local user.email)"

    local_id=("$@")

    if [[ "${current_id[0]}" == "${local_id[0]}" &&
          "${current_id[1]}" == "${local_id[1]}" ]]; then
        printf " Local identity is:\n"
        printf "»  User: %s\n»  Mail: %s\n\n" "${current_id[@]}"
    else
        printf "»  User: %s\n»  Mail: %s\n\n" "${local_id[@]}"
        git config --local user.name "${local_id[0]}"
        git config --local user.email "${local_id[1]}"
    fi

    return 0
}

# -------- IMPLEMENTATION
current_remote_url="$(git config --get --local remote.origin.url)"

if [[ "$current_remote_url" ]]; then

    for service in "${git_remotes[@]}"; do

        # Disable case sensitivity for regex matching
        shopt -s nocasematch

        if [[ "$current_remote_url" =~ $service ]]; then
            case "$service" in

                "${git_remotes[0]}" )
                    printf "\n»» An Intermission\n»  %s repository found." "${git_remotes[0]}"
                    setIdentity "${local_id_0[@]}"
                    exit 0
                    ;;

                "${git_remotes[1]}" )
                    printf "\n»» An Intermission\n»  %s repository found." "${git_remotes[1]}"
                    setIdentity "${local_id_1[@]}"
                    exit 0
                    ;;

                * )
                    printf "\n»  pre-commit hook: unknown error\n» Quitting.\n"
                    exit 1
                    ;;

            esac
        fi
    done
else
    printf "\n»» An Intermission\n»  No remote repository set. Using local fallback identity:\n"
    printf "»  User: %s\n»  Mail: %s\n\n" "${local_fallback_id[@]}"

    # Get the user's attention for a second
    sleep 1

    git config --local user.name "${local_fallback_id[0]}"
    git config --local user.email "${local_fallback_id[1]}"
fi

exit 0

编辑:

所以,我将钩子重写为Python中的钩子和命令。 另外,也能够将脚本做为Git命令调用( git passport )。 也能够在配置文件( ~/.gitpassport )中定义任意数量的ID,这些ID可在提示符下选择。 您能够在github.com上找到该项目: git-passport-一个Git命令并用Python编写的钩子来管理多个Git账户/用户身份


#3楼

Windows环境

若是已在系统中安装了它,则能够从Git Extensions --> Settings --> Global Settings进行修改。

gitextensions-最新发布

右键单击Windows环境中的文件夹/目录以访问这些设置。 在此处输入图片说明

更新如何在2.49版中切换/维护多个设置 如何在2.49版中切换/维护多个设置


#4楼

另外一个选项,以获取git与多个名称/电子邮件的工做是由别名git和使用-c标志覆盖全局和库特定的配置。

例如,经过定义别名:

alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'

要查看它是否有效,只需键入git config user.email

$ git config user.email
name@example.com

除了别名,您还能够在$PATH放置一个自定义git可执行文件。

#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"

这些方法优于特定于存储库的.git/config的优势是,当自定义git程序处于活动状态时,它适用于每一个git存储库。 这样,您能够轻松地在用户/名称之间切换,而无需修改任何(共享)配置。


#5楼

若是您不想使用默认电子邮件地址( 电子邮件地址连接到github用户 ),则能够配置要询问的地址。 如何作到这一点取决于您使用的git版本,请参见下文。

(预期的)缺点是您必须为每一个存储库配置一次电子邮件地址(和名称)。 所以,您不能忘记这样作。

版本<2.7.0

[user]
    name = Your name
    email = "(none)"

在全局配置~/.gitconfig如Dan Aloni在Orr Sella的博客文章中的评论中所述。 尝试在存储库中进行第一次提交时,git失败,并显示一条漂亮消息:

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got '(none)')

当电子邮件地址在本地设置时,该名称是从全局配置中获取的(消息不是很准确)。

2.7.0≤版本<2.8.0

版本低于2.7.0的行为不是预期的,并已用2.7.0修复。 您仍然能够按照Orr Sella的博客文章中所述使用预提交挂钩。 此解决方案也适用于其余版本,但其余解决方案不适用于该版本。

版本≥2.8.0

Dan Aloni添加了实现该行为的选项(请参阅发行说明 )。 搭配使用:

[user]
    useConfigOnly = true

为了使其正常工做,您可能没法在全局配置中提供名称或电子邮件地址。 而后,在第一次提交时,您会收到一条错误消息

fatal: user.useConfigOnly set but no name given

所以,该消息不是颇有启发性,可是因为您显式设置了该选项,所以您应该知道该怎么作。 与版本低于2.7.0的解决方案相比,您始终必须手动设置名称和电子邮件。

相关文章
相关标签/搜索