Can I specify multiple users for myself in .gitconfig?

后端 未结 20 2340
无人及你
无人及你 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:52

    There is a simple solution that seems to work well for avoiding mistakes.

    Simply remove the [user] section from your ~/.gitconfig, which will prevent you from making any commits without setting user.name for each repository.

    In your ~/.bashrc, add some simple aliases for the user and email:

    alias ggmail='git config user.name "My Name";git config user.email me@gmail.com'
    alias gwork='git config user.name "My Name";git config user.email me@work.job'
    
    0 讨论(0)
  • 2020-11-22 06:53

    Another option to get git to work with multiple names / emails is by aliasing git and using the -c flag to override the global and repository-specific config.

    For example, by defining an alias:

    alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'
    

    To see whether it works, simply type git config user.email:

    $ git config user.email
    name@example.com
    

    Instead of an alias, you could also put a custom git executable within your $PATH.

    #!/bin/sh
    /usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"
    

    An advantage of these method over a repository-specific .git/config is that it applies to every git repository when the custom git program is active. In this way, you can easily switch between users/names without modifying any (shared) configuration.

    0 讨论(0)
  • 2020-11-22 06:54

    GIT_AUTHOR_EMAIL + local .bashrc

    .bashrc_local: don't track this file, put it only on your work computer:

    export GIT_AUTHOR_EMAIL='me@work.com'
    export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
    

    .bashrc: track this file, make it the same on both work and home computers:

    F="$HOME/.bashrc_local"
    if [ -r "$F" ]; then
        . "$F"
    fi
    

    I'm using https://github.com/technicalpickles/homesick to sync my dotfiles.

    If only gitconfig would accept environment variables: Shell variable expansion in git config

    0 讨论(0)
  • 2020-11-22 06:54

    Just add this to your ~/.bash_profile to switch between default keys for github.com

    # Git SSH keys swap
    alias work_git="ssh-add -D  && ssh-add -K ~/.ssh/id_rsa_work"
    alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"
    
    0 讨论(0)
  • 2020-11-22 06:56

    With conditional includes in Git 2.13, it is now possible to have multiple user/email coexist on one machine with little work.

    user.gitconfig has my personal name and email. work-user.gitconfig has my work name and email. Both files are at ~ path.

    So my personal name/email applies by default. For c:/work/ dir, my work name/email is applied. For c:/work/github/ dir, my personal name/email is applied. This works as the last setting gets applied.

    # ~/.gitconfig
    [include]
        path = user.gitconfig
    [includeIf "gitdir/i:c:/work/"]
        path = work-user.gitconfig
    [includeIf "gitdir/i:c:/work/github/"]
        path = user.gitconfig
    

    gitdir is case-sensitive and gitdir/i is case-insensitive.

    "gitdir/i:github/" would apply the conditional include for any directory with github in its path.

    0 讨论(0)
  • 2020-11-22 06:56

    Windows Environment

    Additional this can be modified from Git Extensions --> Settings --> Global Settings, if you have it installed in your systems.

    gitextensions-latest-release

    Right Click on a folder/directory in Windows Environment to access these settings. enter image description here

    Update : How to switch/maintain multiple settings in Version 2.49

    0 讨论(0)
提交回复
热议问题