Can I specify multiple users for myself in .gitconfig?

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 started using git for work, too. My company’s git repo allows me to commit, but when it sends out announcements of new changesets, it says they are from Anonymous because it doesn’t recognize the email address in my .gitconfig – at least, that’s my theory.

  • Cloak/Exclude folders in Source Settings in tfs 2013 build definition from Team Foundation Git repository
  • git stash and edited hunks
  • Include utility repository in main project repository and allow customization
  • Git as a backup and Version Control System
  • Please review my config. My vimdiff not working with GIT after setup
  • What does hg bookmark mean in this context?
  • Is it possible to specify multiple [user] definitions in .gitconfig? Or is there some other way to override the default .gitconfig for a certain directory? In my case, I check out all work code in ~/worksrc/ – is there a way to specify a .gitconfig for only that directory (and its subdirectories)?

  • Get the changes made in a specific file at a specific commit?
  • JGit create new local branch and push to remote (branch does not exist on remote)
  • How can I limit the log to all the descendants of a given commit?
  • Best practice for Composer branch aliases and tags
  • Git - Accidentally Overwrote Entire Commit History of Remote Repository
  • Approach to handling such code change with Git
  • 16 Solutions collect form web for “Can I specify multiple users for myself in .gitconfig?”

    You can configure an individual repo to use a specific user / email address which overrides the global configuration. From the root of the repo, run

    git config "Your Name Here"
    git config

    whereas the default user / email is configured in your ~/.gitconfig

    git config --global "Your Name Here"
    git config --global

    Or you can add following information in your local .git/config file

        name = Your Name
        email =

    Since git 2.13, it is possible to solve this using newly introduced Conditional includes.

    An example:

    Global config ~/.gitconfig

        name = John Doe
        email = john@doe.tld
    [includeIf "gitdir:~/work/"]
        path = ~/work/.gitconfig

    Work specific config ~/work/.gitconfig

        email = john.doe@company.tld

    After getting some inspiration from Orr Sella’s blog post I wrote a pre-commit hook (resides in ~/.git/templates/hooks) which would set specific usernames and e-mail addresses based on the information inside a local repositorie’s ./.git/config:

    You have to place the path to the template directory into your ~/.gitconfig:

        templatedir = ~/.git/templates

    Then each git init or git clone will pick up that hook and will apply the user data during the next git commit. If you want to apply the hook to already exisiting repos then just run a git init inside the repo in order to reinitialize it.

    Here is the hook I came up with (it still needs some polishing – suggestions are welcome).
    Save it either as




    and make sure it is executable: chmod +x ./post-checkout || chmod +x ./pre_commit

    #!/usr/bin/env bash
    # -------- USER CONFIG
    # Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
    # Adjust names and e-mail addresses
    # -------- FUNCTIONS
        local current_id local_id
        current_id[0]="$(git config --get --local"
        current_id[1]="$(git config --get --local"
        if [[ "${current_id[0]}" == "${local_id[0]}" &&
              "${current_id[1]}" == "${local_id[1]}" ]]; then
            printf " Local identity is:\n"
            printf "»  User: %s\n»  Mail: %s\n\n" "${current_id[@]}"
            printf "»  User: %s\n»  Mail: %s\n\n" "${local_id[@]}"
            git config --local "${local_id[0]}"
            git config --local "${local_id[1]}"
        return 0
    # -------- IMPLEMENTATION
    current_remote_url="$(git config --get --local remote.origin.url)"
    if [[ "$current_remote_url" ]]; then
        for service in "${git_remotes[@]}"; do
            # Disable case sensitivity for regex matching
            shopt -s nocasematch
            if [[ "$current_remote_url" =~ $service ]]; then
                case "$service" in
                    "${git_remotes[0]}" )
                        printf "\n»» An Intermission\n»  %s repository found." "${git_remotes[0]}"
                        setIdentity "${local_id_0[@]}"
                        exit 0
                    "${git_remotes[1]}" )
                        printf "\n»» An Intermission\n»  %s repository found." "${git_remotes[1]}"
                        setIdentity "${local_id_1[@]}"
                        exit 0
                    * )
                        printf "\n»  pre-commit hook: unknown error\n» Quitting.\n"
                        exit 1
        printf "\n»» An Intermission\n»  No remote repository set. Using local fallback identity:\n"
        printf "»  User: %s\n»  Mail: %s\n\n" "${local_fallback_id[@]}"
        # Get the user's attention for a second
        sleep 1
        git config --local "${local_fallback_id[0]}"
        git config --local "${local_fallback_id[1]}"
    exit 0


    So I rewrote the hook as a hook and command in Python. Additionally it’s possible to call the script as a Git command (git passport), too. Also it’s possible to define an arbitrary number of IDs inside a configfile (~/.gitpassport) which are selectable on a prompt. You can find the project at git-passport – A Git command and hook written in Python to manage multiple Git accounts / user identities.

    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

        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.
      git config --global ""
      git config --global "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:

        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.

    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"Your name" -c""'

    To see whether it works, simply type git config

    $ git config

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

    /usr/bin/git -c"Your name" -c"" "$@"

    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.

    One command github accounts switch

    This solution takes the form of a single git alias. Once executed, the current project user will be attached to another account

    Generate ssh keys

    ssh-keygen -t rsa -C "" -f '/Users/arnaudrinquin/.ssh/id_rsa'
    ssh-keygen -t rsa -C "" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'

    Link them to your GitHub / Bitbucket accounts

    1. copy default public key pbcopy < ~/.ssh/
    2. login to your GitHub acount
    3. paste the key in the add SSH key github page
    4. copy other public key pbcopy < ~/.ssh/
    5. repeat and adapt steps 2 to 4 for every other account

    Step 1. Automatic ssh key switching.

    We can configure ssh to send a use a specific encryption key depending on the host. The nice thing is that you can have several aliases for the same hostname.

    See this example ~/.ssh/config file:

    # Default GitHub
      User git
      IdentityFile ~/.ssh/id_rsa
    # Professional github alias
    Host github_pro
      User git
      IdentityFile ~/.ssh/id_rsa_pro

    git remote configuration

    You can now use these aliases in the git remotes by changing by git@github_pro.

    You can either change your existing projects remotes (using something like git remote origin set-url git@github_pro:foo/bar.git) or adapt them directly when cloning them.

    git clone

    using alias, it become:

    git clone git@github_pro:ArnaudRinquin/atom-zentabs.git

    Step 2. Changing git

    Git config settings can be global or per project. In our case, we want a per project settings. It is very easy to change it:

    git config ''

    While this is easy, it takes way to long for the developers we are. We can write a very simple git alias for that.

    We are going to add it to the ~/.gitconfig file.

        name = Arnaud Rinquin
        email =
        setpromail = "config ''"

    Then, all we have to do is git setpromail to have our email changed for this project only.

    Step 3. One command switch please?!

    Wouldn’t it be nice to switch from default account to a specified one with a single parameter-less command? This is definitely possible. This command will have two steps:

    • change current project remotes to the chosen aliases
    • change current project config

    We already have a one command solution for the second step, but the first one is way harder.
    One command remote host change

    Here comes the solution in the form of another git alias command to add to your ~/.gitconfig:

      changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"

    This allows changing all remotes from one host to another (the alias). See the example:

    $ > git remote -v
    origin (fetch)
    origin (push)
    $ > git changeremotehost github_pro
    $ > git remote -v
    origin  git@github_pro:ArnaudRinquin/ (fetch)
    origin  git@github_pro:ArnaudRinquin/ (push)

    Combine them all

    We now just have to combine the two commands into one, this is quite easy. See how I also integrate bitbucket host switching.

      changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"
      setpromail = "config ''"
      gopro = !sh -c \"git changeremotehost github_pro && git changeremotehost bitbucket_pro && git setpromail\"

    Source Link -Tutorial

    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 for each repository.

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

    alias ggmail='git config "My Name";git config'
    alias gwork='git config "My Name";git config me@work.job'

    git aliases (and sections in git configs) to the rescue!

    add an alias (from command line):

    git config --global alias.identity '! git config $(git config user.$; git config $(git config user.$; :'

    then, set, for example

    git config --global "your github username"
    git config --global

    and in a new or cloned repo you can run this command:

    git identity github

    This solution isn’t automatic, but unsetting user and email in your global ~/.gitconfig would force git to remind you to set them manually in each new or cloned repo.

    git config --global --unset
    git config --global --unset

    GIT_AUTHOR_EMAIL + local .bashrc

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

    export GIT_AUTHOR_EMAIL=''

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

    if [ -r "$F" ]; then
        . "$F"

    I’m using to sync my dotfiles.

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

    This answer is partially inspired by the post by @Saucier, but I was looking for an automated way to set and on a per repo basis, based on the remote, that was a little more light weight than the git-passport package that he developed. Also h/t to @John for the useConfigOnly setting. Here is my solution:

    .gitconfig changes:

        name = <github username>
        email = <github email>
        name = <gitlab username>
        email = <gitlab email>
        templatedir = ~/.git-templates
        useConfigOnly = true

    post-checkout hook which should be saved to the following path: ~/.git-templates/hooks/post-checkout:

    #!/usr/bin/env bash
    # make regex matching below case insensitive
    shopt -s nocasematch
    # values in the services array should have a corresponding section in
    # .gitconfig where the 'name' and 'email' for that service are specified
    remote_url="$( git config --get --local remote.origin.url )"
    set_local_user_config() {
        local service="${1}"
        local config="${2}"
        local service_config="$( git config --get ${service}.${config} )"
        local local_config="$( git config --get --local user.${config} )"
        if [[ "${local_config}" != "${service_config}" ]]; then
            git config --local "user.${config}" "${service_config}"
            echo "repo 'user.${config}' has been set to '${service_config}'"
    # if remote_url doesn't contain the any of the values in the services
    # array the user name and email will remain unset and the
    # user.useConfigOnly = true setting in .gitconfig will prompt for those
    # credentials and prevent commits until they are defined
    for s in "${services[@]}"; do
        if [[ "${remote_url}" =~ "${s}" ]]; then
            set_local_user_config "${s}" 'name'
            set_local_user_config "${s}" 'email'

    I use different credentials for github and gitlab, but those references in the code above could be replaced or augmented with any service that you use. In order to have the post-checkout hook automatically set the user name and email locally for a repo after a checkout make sure the service name appears in the remote url, add it to the services array in the post-checkout script and create a section for it in your .gitconfig that contains your user name and email for that service.

    If none of the service names appear in the remote url or the repo doesn’t have a remote the user name and email will not be set locally. In these cases the user.useConfigOnly setting will be in play which will not allow you to make commits until the user name and email are set at the repo level, and will prompt the user to configure that information.

    Windows Environment

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


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

    May be it is simple hack, but it is useful. Just generate 2 ssh keys like below.

    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/GowthamSai/.ssh/id_rsa): work
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in damsn.
    Your public key has been saved in
    The key fingerprint is:
    SHA256:CrsKDJWVVek5GTCqmq8/8RnwvAo1G6UOmQFbzddcoAY GowthamSai@Gowtham-MacBook-Air.local
    The key's randomart image is:
    +---[RSA 4096]----+
    |. .oEo+=o+.      |
    |.o o+o.o=        |
    |o o o.o. +       |
    | =.+ .  =        |
    |= *+.   S.       |
    |o*.++o .         |
    |=.oo.+.          |
    | +. +.           |
    |.o=+.            |

    Same way create one more for personal. So, you have 2 ssh keys, work and company. Copy, work,, personal to ~/.ssh/ Directory.

    Then create shell script with the following lines and name it as (Company Reverse) with following content.

    cp ~/.ssh/work ~/.ssh/id_rsa
    cp ~/.ssh/ ~/.ssh/

    Same way, create one more called (Personal Reverse) with the following content.

    cp ~/.ssh/personal ~/.ssh/id_rsa
    cp ~/.ssh/ ~/.ssh/

    in ~/.bashrc add aliases for those scripts like below

    alias crev="sh ~/.ssh/"
    alias prev="sh ~/.ssh/"
    source ~/.bashrc

    When ever you wanna use company, just do crev, and if you wanna use personal do prev :-p.

    Add those ssh keys to your github accounts. Make sure, you don’t have id_rsa generated perviously, because those scripts will overwrite id_rsa. If you have already generated id_rsa, use that for one of the account. Copy them as personal and skip generation of personal keys.

    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
        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.

    Something like Rob W’s answer, but allowing different a different ssh key, and works with older git versions (which don’t have e.g. a core.sshCommand config).

    I created the file ~/bin/git_poweruser, with executable permission, and in the PATH:

    TMPDIR=$(mktemp -d)
    trap 'rm -rf "$TMPDIR"' EXIT
    cat > $TMPDIR/ssh << 'EOF'
    ssh -i $HOME/.ssh/poweruserprivatekey $@
    chmod +x $TMPDIR/ssh
    export GIT_SSH=$TMPDIR/ssh
    git -c"Power User name" -c"" $@

    Whenever I want to commit or push something as “Power User”, I use git_poweruser instead of git. It should work on any directory, and does not require changes in .gitconfig or .ssh/config, at least not in mine.

    To be able to update and commit to git projects from different organization, add another email to your ~/.gitconfig file. If you don’t have any such file, create one. The file should look like this:

        name = Bob Will
        email =
        email =
    Git Baby is a git and github fan, let's start git clone.