Can I specify multiple users for myself in .gitconfig?

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

    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 "rinquin.arnaud@gmail.com" -f '/Users/arnaudrinquin/.ssh/id_rsa'
    
    [...]
    
    ssh-keygen -t rsa -C "arnaud.rinquin@wopata.com" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'
    

    Link them to your GitHub / Bitbucket accounts

    1. copy default public key pbcopy < ~/.ssh/id_rsa.pub
    2. login to your GitHub acount
    3. paste the key in the add SSH key github page
    4. copy other public key pbcopy < ~/.ssh/id_rsa_pro.pub
    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
    Host github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa
    
    # Professional github alias
    Host github_pro
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_pro
    

    git remote configuration

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

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

    git clone git@github.com:ArnaudRinquin/atom-zentabs.git
    

    using alias, it become:

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

    Step 2. Changing git user.email

    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 user.email 'arnaud.rinquin@wopata.com'
    

    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.

    [user]
        name = Arnaud Rinquin
        email = rinquin.arnaud@gmail.com
    
    ...
    
    [alias]
        setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
    

    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 user.email 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:

    [alias]
      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  git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
    origin  git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (push)
    
    $ > git changeremotehost github.com github_pro
    
    $ > git remote -v
    origin  git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
    origin  git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (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.

    [alias]
      changeremotehost = !sh -c \"git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url\"
      setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
      gopro = !sh -c \"git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail\"
    

    Source Link -Github

    Source Link -Tutorial

提交回复
热议问题