Can I specify multiple users for myself in. gitconfig?

Keywords: git github Python Windows

At ~ /. gitconfig, I have my personal email address at [user], because this is the address I want to use for the Github repository.

However, I recently started working with GIT. My company's git repo allows me to submit, but when it posts new changesets, it says they come from anonymous users because it doesn't recognize my. gitconfig email address - at least, that's my theory.

Can I specify multiple [user] definitions in. gitconfig? Or is there another way to override the default. gitconfig for a directory? As far as I'm concerned, I check out all the working code at ~ / worksrc / - is there a way to specify. gitconfig only for that directory (and its subdirectories)?

#1 building

Alternatively, you can add the following information to your local. git/config file

[user]  
    name = Your Name
    email = your.email@gmail.com

#2 building

from In Orr Sella's blog post Get some inspiration Later, I wrote a pre commit hook (located in ~ /. git/templates/hooks) that sets a specific user name and email address based on the information in the local repository. / git / config. ./.git/config :

You must put the path of the template directory in ~ /. gitconfig:

[init]
    templatedir = ~/.git/templates

Then, each git init or git clone selects the hook and applies the user data during the next git commit. If you want to apply the hook to an existing repository, simply run git init in the repository to reinitialize it.

This is the hook I came up with (still needs polishing - welcome to make suggestions). Save as

~/.git/templates/hooks/pre_commit

Or

~/.git/templates/hooks/post-checkout

And make sure it's executable: Chmod + X. / post checkout | Chmod + X. / pre commit 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
git_remotes[0]="Github"
git_remotes[1]="Gitlab"

# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"

local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"

local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"


# -------- FUNCTIONS
setIdentity()
{
    local current_id local_id

    current_id[0]="$(git config --get --local user.name)"
    current_id[1]="$(git config --get --local user.email)"

    local_id=("$@")

    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[@]}"
    else
        printf "»  User: %s\n»  Mail: %s\n\n" "${local_id[@]}"
        git config --local user.name "${local_id[0]}"
        git config --local user.email "${local_id[1]}"
    fi

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

            esac
        fi
    done
else
    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 user.name "${local_fallback_id[0]}"
    git config --local user.email "${local_fallback_id[1]}"
fi

exit 0

Editor:

So, I rewrite hooks as hooks and commands in Python. Alternatively, you can call the script as a Git command (git passport). You can also define any number of ID S in the configuration file (~ /. gitpassport), which can be selected at the prompt. You can find the project on github.com: Git passport - a git command and hook written in Python to manage multiple git accounts / user identities .

#3 building

Windows Environment

If it is already installed on your system, you can modify it from git extensions > Settings > global settings.

gitextensions - latest release

Right click the folder / directory in the Windows environment to access these settings.

Update: how to switch / maintain multiple settings in version 2.49

#4 building

Another option to get git working with multiple names / emails is to override the global and library specific configuration by aliasing GIT and using the - c flag.

For example, by defining an alias:

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

To see if it works, just type git config user.email:

$ git config user.email
name@example.com

In addition to aliases, you can place a custom git executable at $PATH.

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

The advantage of these methods over repository specific. git/config is that when a custom git program is active, it works for each git repository. This allows you to easily switch between users / names without modifying any (shared) configuration.

#5 building

If you do not want to use the default email address( Email address links to github users ), you can configure the address to ask. How you do this depends on the version of git you are using, see below.

The (expected) disadvantage is that you must configure the email address (and name) once for each repository. Therefore, you cannot forget to do so.

Version <2.7.0

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

In global configuration ~ /. gitconfig, such as Dan Aloni in Orr Sella's blog post As described in the comments in. When you try to commit for the first time in the repository, git fails with a 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)')

When the email address is set locally, the name is obtained from the global configuration (the message is not very accurate).

2.7.0 ≤ version < 2.8.0

Behavior below version 2.7.0 is not expected and has been fixed with 2.7.0. You can still follow the In Orr Sella's blog post Use the pre submission hook as described. This solution also works with other versions, but other solutions do not work with that version.

Version ≥ 2.8.0

Dan Aloni add Options to implement this behavior (see Release notes ) Use with:

[user]
    useConfigOnly = true

For it to work, you may not be able to provide a name or email address in the global configuration. Then, on the first commit, you receive an error message

fatal: user.useConfigOnly set but no name given

So the message is not very illuminating, but because you explicitly set this option, you should know what to do. You always have to manually set the name and email compared to a solution that is less than 2.7.0.

Posted by raven_web on Tue, 24 Dec 2019 01:52:29 -0800