Is it possible to configure user.name and user.email per wildcard domains in .gitconfig?

前端 未结 5 505
不思量自难忘°
不思量自难忘° 2021-01-31 03:00

I have a work computer, and it\'s configured globally to use my work email and name when committing. This is good. However, I\'d like to make some sort of rule that says, \"if t

5条回答
  •  庸人自扰
    2021-01-31 03:14

    There is nothing built into Git to do this (as far as I know), but the following shell script seems to work pretty reliably. Modify it to have the user name and email address you want when checking into GitHub, and then save it as an executable named "git" on your path somewhere before the "real" git.

    #!/usr/bin/env bash
    
    # "Real" git is the second one returned by 'which'
    REAL_GIT=$(which -a git | sed -n 2p)
    
    # Does the remote "origin" point to GitHub?
    if ("$REAL_GIT" remote -v 2>/dev/null |
        grep '^origin\b.*github.com.*(push)$' >/dev/null 2>&1); then
    
        # Yes.  Set username and email that you use on GitHub.
        export GIT_AUTHOR_NAME='*** put your name here ***'
        export GIT_AUTHOR_EMAIL='*** put your email address here ***'
    
    fi
    
    "$REAL_GIT" "$@"
    

    I use a similar trick with ssh on my machine -- I want ssh to change my window background color when it runs, and then change it back when it exits -- and it has worked reliably for me.

    Also, note that this is hard-coded to only look at the remote that is named origin.

提交回复
热议问题