2011-01-08 66 views
6

我使用多個Git遠程存儲庫,每個需要不同的Git憑據(名稱和郵件)。如何在Git中管理多個用戶配置?

有沒有解決方案,如腳本或最佳實踐如何管理它們?

我知道「config --local」,但我不想每次手動設置這個變量。

+0

另請參閱[我可以在.gitconfig中爲自己指定多個用戶嗎?](http:// stackoverflow。com/questions/4220416/can-i-specify-gitconfig -in-myself-in-gitconfig) – 2014-05-19 01:46:34

回答

10

它看起來像只是說在一個特定的倉庫git config user.name(或user.email),而不指定--global--system會做的伎倆。默認設置是在當前的存儲庫中設置配置,並且必須爲其提供明確的選項才能寫入您的用戶級別或系統範圍的配置。

不知道如果你剛剛克隆需要不同配置的存儲庫,人們會怎麼做。也許你可以編寫一個包含git clone的小腳本來克隆一些存儲庫,然後根據任何信息設置適當的配置?如果您放棄/usr/lib/git-core中名爲git-nclone的腳本,則可以將其作爲git nclone運行。

編輯:既然你不想每次都手動設置它,那麼克隆包裝器如何記住你實際使用的各種集合,並且可以爲你正在克隆的存儲庫選擇合適的包裝。它甚至可以根據您的克隆來自哪裏進行智能默認設置。

+1

在.git/config文件中出現對本地配置的更改 – Abizern 2011-01-08 19:31:28

0

我創建了幾個別名,看起來像這樣:

的想法是,我可以救我的憑據(電子郵件,用戶名)的別名定義。然後當我想克隆或初始化時,我不必每次都執行git config

初始化時:

initgithub = !git init && git config user.email [[email protected]] && git config user.name [yourgithubusername] 

initbitbucket = !git init && git config user.email [[email protected]] && git config user.name [yourbitbucketusername] 

克隆時:

clonegithub = "!f() { git clone $1 $2; cd $2; git config user.email [[email protected]]; git config user.name [yourgithubusername]; }; f" 

clonebitbucket = "!f() { git clone $1 $2; cd $2; git config user.email [[email protected]]; git config user.name [yourbitbucketusername]; }; f" 

ussage:

初始化時:

GIT中initgithub

GIT中initbitbucket

克隆時:

GIT中clonegithub https://github.com/pathtoproject.git/C /溫度/ somefolder /項目

GIT中clonebitbucket https://github.com/pathtoproject.git/C /溫度/ somefolder /項目

克隆時,基本上可以創建一個函數,該函數將執行正常的克隆操作和配置操作。目前,它要求您提供要克隆到的文件夾的路徑,以便正確配置您的憑證。