Can I specify multiple users for myself in .gitconfig?

后端 未结 20 2600
无人及你
无人及你 2020-11-22 06:33

In my ~/.gitconfig, I list my personal email address under [user], since that\'s what I want to use for Github repos.

But, I\'ve recently s

20条回答
  •  耶瑟儿~
    2020-11-22 06:45

    If you do not want to have a default email address (email address links to a github user), you can configure that you want to be asked. How you can do that depends on the version of git you use, see below.

    The (intended) drawback is that you have to configure your email address (and your name) once for every repository. So, you cannot forget to do it.

    Version < 2.7.0

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

    in your global configuration ~/.gitconfig as stated in a comment by Dan Aloni in Orr Sella's blog post. When trying to do the first commit in a repository, git fails with the nice message:

    *** 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)')
    

    The name is taken from the global config when the email address is set locally (the message is not perfectly accurate).

    2.7.0 ≤ Version < 2.8.0

    The behaviour in versions < 2.7.0 was not intended and fixed with 2.7.0. You can still use a pre-commit hook as described in Orr Sella's blog post. This solution works also for other versions, but the other solutions not for this version.

    Version ≥ 2.8.0

    Dan Aloni added an option to achieve that behaviour (see release notes). Use it with:

    [user]
        useConfigOnly = true
    

    To make it work you may not give a name or email address in the global config. Then, at the first commit, you get an error message

    fatal: user.useConfigOnly set but no name given
    

    So the message is not very instructive, but since you set the option explicitly, you should know what to do. In contrast to the solution of versions < 2.7.0, you always have to set both name and email manually.

提交回复
热议问题