Generating an SSH Public Key

By default, users’ SSH keys are stored in their ~/.ssh directory. Go to this directory and list the contents to quickly verify that you have a key:

$ cd ~/.ssh
$ ls
authorized_keys2  id_dsa       known_hosts
config            id_dsa.pub
Copy the code

One of them has the.pub extension. The.pub file is your public key and the other is the corresponding private key.

Define the name of the male key

Since we need to generate public keys for each environment (GitLab, Github), we need to customize the names

SSH /customFileName -c "email address" // For example, ssh-keygen -f ~/. SSH /github_rsa -c "my.163.com" // Github key public key SSH /gitlab_rsa -c "my.163.com" // gitlab key public keyCopy the code

Add public keys to Github and GitLab

SSH /github_rsa and ~/.ssh/gitlab_rsa public keys are copied and added to Github and GitLab.

github gitlab

Use different keys depending on the domain name definition

To manage multiple keys, create a config file ~/. SSH /config in the corresponding folder to add keys for different domain names

Git IdentityFile ~/.ssh/id_rsa.gitlab # githab Host github.com HostName github.com User git IdentityFile ~/.ssh/github_rsaCopy the code

git config

Git comes with a Git config tool to help set configuration variables that control how Git looks and behaves. These variables are stored in three different locations:

  • /etc/gitconfig file: contains the common configuration for each user and their repository on the system.
  • ~/. Gitconfig or ~/. Config /git/config files: only for the current user.
  • The config file in the Git directory of the repository currently in use (i.e..git/config) : for this repository.

I want to configure different git user information configurations for different projects on my computer. Add the following configuration to ~/. Gitconfig:

[includeIf "gitdir:/Users/huifei/Documents/company/"]
    path = .gitconfig-work
[includeIf "gitdir:/Users/huifei/Documents/try/"]
    path = .gitconfig-self
Copy the code

As you can see, the projects I work on are all in the company directory, and they are all connected to Gitlab, while the try directory is connected to Github. I set them to.gitconfig-work and.gitconfig-self, respectively, according to the path.

# .gitconfig-work 
[user]
    name = gitlabname
    email = youremail
# .gitconfig-self
[user]
    name = githuabname
    email = youremail
Copy the code

reference

git config