Git set multiple accounts

Recently, I changed my company, and I encountered some problems in setting git account. Simply speaking, I wanted to make some fish at work and found that the submission record was the company’s account, which of course was not very good.

If you work for a company, you will be assigned an email address and there are certain rules for submitting Git. For example, you must use your own name so that it is easy to know who submitted the code. And they also have their own private accounts, even accounts on different platforms, such as Gitee and Github:

// company - gitlab
name=xiaoming
[email protected]

// personal - github
name=DoctorX
[email protected]
Copy the code

Normally a global name is set as the account for submission:

Set global account
git config --global user.name "xiaoming"
git config --global user.email "[email protected]"

# check configuration
git config --global user.name # xiaoming
git config --global user.email # [email protected]
Copy the code

Each commit will default to the name of the global setting.

The problem

The default commit username is xiaoming, and the default commit username is xiaoming.

  • Platform use within the companyxiaoming
  • Github for personal projectsDoctorX

The solution

When connecting to git server, SSH key is usually set for security authentication, but by default, the generated key overwrites the previous Settings (the file name is the same), so we need to set different key files for different users

  1. Generate an ssh-key for the company
ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitlab_id_rsa
Copy the code
  1. Generate a github ssh-key
ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/github_id_rsa
Copy the code
  1. in~/.sshCreate a config file in the directory and add the following information
# gitlab
Host gitlab.com
HostName gitlab.com
User xiaoming
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa

# github
Host github.com
HostName github.com
User DoctorX
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
Copy the code

In this way, when accessing the remote server, different keys are used for authentication based on different addresses.

When you open ~/.ssh, you will see:

config
github_id_rsa
github_id_rsa.pub
gitlab_id_rsa
gitlab_id_rsa.pub
Copy the code

Next, set the public keys in the.pub file to gitlab and Githuh, respectively.

Test whether the setting is successful:

ssh -T [email protected]
ssh -T [email protected]
Copy the code

Finally, clear the user name and mailbox of global Settings

git config --global --unset user.name 
git config --global --unset user.email
Copy the code

Project Settings

If you commit your project, you will be prompted to commit with your computer’s account name

The part of the Mosaic is my PC account name

In this case, you need to configure the local level account information for each project and run the following command in the corresponding project

git config --local user.name "xxxx"
git config --local user.email "xxxx"
Copy the code

Then modify the submitter information again

git commit --amend --reset-author
Copy the code

Git config –local –list git config –local –list

Of course, it’s a hassle to type two sentences for each item, so write a global script to do it

#! /bin/bash

type=The $1
dir=$(pwd)

case ${type} in
  gitlab)
    git config --local user.name user1
    git config --local user.email xxx
    echo "select [${type}] and set in [${dir}]. ""
    ;;
  gitee)
    git config --local user.name user2
    git config --local user.email xxx
    echo "select [${type}] and set in [${dir}]. ""
    ;;
  github)
    git config --local user.name user3
    git config --local user.email xxx
    echo "select [${type}] and set in [${dir}]. ""
    ;;
  *)
    echo "${type} is invalid!"
    exit1;;esac
Copy the code

Then add it to an environment variable or global alias, just run it!