我能夠在.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的解決方案相比,您始終必須手動設置名稱和電子郵件。

相關文章
相關標籤/搜索