Can I specify multiple users for myself in .gitconfig?

后端 未结 20 2341
无人及你
无人及你 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 07:10

    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 user.name "Your Name Here"
    git config user.email your@email.com
    

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

    git config --global user.name "Your Name Here"
    git config --global user.email your@email.com
    
    0 讨论(0)
  • 2020-11-22 07:12

    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:

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

    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.

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