2017-02-04 225 views
4

我在不同的計算機上密集使用不同用戶名的Git。有時候,當我克隆一個倉庫,我忘記配置user.nameuser.email,當我犯了第一個提交到本地倉庫沒有指定的標識名稱,我得到如果未配置user.name和user.email,則在Git中中止提交

Your name and email address were configured automatically based 
on your username and hostname. Please check that they are accurate. 
You can suppress this message by setting them explicitly: 

    git config --global user.name "Your Name" 
    git config --global user.email [email protected] 

After doing this, you may fix the identity used for this commit with: 

    git commit --amend --reset-author 

是的,這是完全清楚和爲什麼Git這樣說。 (但是,如果我注意到它。)但是,是否可以抑制這種行爲並使Git不會將我的用戶名和主機名作爲提交作者用戶名傳播?爲什麼我問的問題是,我的[user] name =[user] email =都沒有在我的~/.gitconfig中:我只是針對不同的存儲組使用不同的用戶名和電子郵件,我希望Git只是在無法獲得用戶名時中止提交來自配置的電子郵件。最糟糕的是我有一個自定義的Git-informative PS1配置顯示當前的存儲庫用戶名和電子郵件的權利,因爲我有歷史重寫和強制推送幾次,甚至沒有注意到巨大的Git警告用戶名默認。

如果全局未指定user.nameuser.email,是否可以放棄Git中的提交? (我想它可以通過掛鉤是固定的,但掛鉤是本地唯一的,對吧?)

回答

6

是,user.useConfigOnly

git config --global user.useConfigOnly true 

git-config(1)

指示的Git避免試圖猜測默認值爲user.emailuser.name,而是僅從配置中檢索 值。例如,如果您有多個電子郵件地址並且想要爲每個存儲庫使用不同的電子郵件地址,則在全局配置選項 配置中將此配置選項設置爲true以及名稱,Git將提示您在設置電子郵件之前設置電子郵件在新的 克隆存儲庫中提交新提交。默認爲false。

+0

雖然我希望它是簡單的。我感到羞愧,因爲我無法看到這個選項,以及我無法有時閱讀我的​​'PS1'......謝謝! –

+0

有趣的細節:'user.useConfigOnly'似乎不起作用,如果'.gitconfig'中的'user ='和'email ='沒有值(我的意思是'= \ n')並且沒有被註釋掉。 –