3

I have multiple accounts on different public git hostings (github, gitlab, etc) and corporate git hosting.

The problem is that I constantly forget to change my identity:

git config --local user.email "email@example.com"
git config --local user.name "Name"

for each project and my default global configuration (github account) goes into my corporate commits and commits on other git hostings (and even into commit from another account on github) creating a real mess.

Is there any convenient method to stop forgetting to specify account after git clone?

  • 1
    Don't run `git clone` directly; use wrapper functions/scripts/etc that run `git clone` *and* do the necessary local configuration. – chepner Jul 31 '19 at 14:36

3 Answers3

4

You could unset your global configuration and always apply the user config locally. When you forget to do so, you will get a warning message telling you to configure it.

You could make aliases to help you (untested):

git config --global alias.personal '!git config --local user.email "home@example.com"; git config --local user.name "Name"'
git config --global alias.work '!git config --local user.email "work@example.com"; git config --local user.name "Name"'

So you could then do:

git clone [work project]
cd [work project]
git work
git clone [home project]
cd [home project]
git home

Ideally you could change the built in behaviour of git clone with an alias, but that's not possible. There are workarounds here: https://stackoverflow.com/a/24266749/3408, so you could use that to change git clone to a script that prompted you to choose an identity after the clone operation.

rjmunro
  • 27,203
  • 20
  • 110
  • 132
  • Thanks for this great answer! Actually the change of behavior of `git clone` is not really necessary, because when global configuration unset, you still receive a warning when you make `git commit`. Moreover, it is boring to setup identity for projects when you not supposed to make any commits. –  Jul 31 '19 at 14:56
0

The best is not to inform them like that when you do a sweater or push, it asks you to authenticate and you can enter the account that you like the development you could do. Could it help you?

ArthurB
  • 1
  • 1
  • 1
    Could you explain how to setup git in such a way? I thought the only possible way is to specify `user.name` and `user.email` –  Jul 31 '19 at 14:02
  • 1
    What is a sweater? – rjmunro Jul 31 '19 at 14:42
  • Sorry bad traduction not sweater but pull sirry – ArthurB Aug 01 '19 at 15:16
  • You could make aliases for configuration : git config --global alias.personal '!git config --local user.email "home@example.com"; git config --local user.name "Name"' git config --global alias.work '!git config --local user.email "work@example.com"; git config --local user.name "Name"' So you could then do: git clone [work project] cd [work project] git work git clone [home project] cd [home project] git home – ArthurB Aug 01 '19 at 15:18
0

You can also set the credentials project basis.

In the terminal inside your project root directory:

Set a Git username:

$ git config user.name "Some Name"
Confirm that you have set the Git username correctly:

$ git config user.name
> Some Name

This credentials will only work for that particular repository.

Kulshreshth K
  • 1,096
  • 1
  • 12
  • 28
  • Yes, I know that I should do exactly this! The problem is that I always forget to do it, that is why I asked the question =). –  Jul 31 '19 at 14:24