Configure multiple git accounts locally (company, GitHub)

When the company explores new knowledge (fishing), we generally need to maintain our own small warehouse on GitHub.
In this way, if the original configuration is used, Permission Denied will occur when pushing the code.
So we have the following steps:
The company's computer is a Mac, so the following contents are all MAC operations, but different operating systems are basically the same.

Different warehouses are configured with different submitted user information

We mainly submit the company code on the company computer, so for convenience, the global user information of git can be set to the name and email required by the company.
Configuring git account information in your own warehouse can overwrite the global.

cd <Your warehouse address>
git config <Yours GitHub Account name>
git config <Yours GitHub Account mailbox>

Generate different ssh keys

# -C followed by email address, for example: - C“ "
# -f followed by the path to generate the key (including the file name of the key), so you don't have to enter the key file name separately during generation
# Different key file names should be distinguished. The following is an example of GitHub's ssh key
ssh-keygen -t rsa -C "<GitHub mailbox>" -f ~/.ssh/id_rsa_github

# Just go back after that

After entering the ssh folder, you can see that I have generated two locally. One is the default name id_rsa, one is the ID of GitHub_ rsa_ GitHub, and their public keys.

Create ssh profile

In the figure above, you can see that there is a config file in my ssh folder. If you have never configured it, you may not have this file locally.

vim ~/.ssh/config

# vim novice tutorial
# Press the i key to enter the insertion mode (content can be entered only after entering the insertion mode), and the word INSERT is displayed in the lower left corner
# After input, press ESC to highlight the insertion mode, and the lower left INSERT disappears
# Press the: (English) key to enter the command mode, and the cursor moves to the bottom of the window
# Enter wq to save and exit (w: save, q: exit, can be used separately)

# The following contents are inserted into the config file. Multiple groups can be configured for multiple accounts. It is recommended to interval one line. Here, only GitHub is an instance
User git
IdentityFile ~/.ssh/id_rsa_github

# Host is the alias when ssh is used. It will be used later when configuring the remote address of the warehouse
# HostName is the domain name of the remote warehouse
# The User is usually git. It doesn't seem to have much impact to fill in other items
# IdentityFile is the corresponding ssh key file generated

Add ssh public key in remote warehouse

GitHub official tutorial

# Print the public key to the command line for easy copying
cat ~/.ssh/
  1. In the upper right corner of any page, click your profile photo, and then click Settings.
  2. In the user settings sidebar, click SSH and GPG keys.
  3. Click New SSH key or Add SSH key.
  4. Paste the content of the public Key just copied into the Key in the figure below, and write the descriptive content of the Title to facilitate you to understand the Key

Test ssh link

ssh -T
# Here - T is followed by the Host alias in the previous config file

# If you see the following, you have succeeded
# Hi < your GitHub account name >! You've successfully authenticated, but GitHub does not provide shell access.

Configure warehouse remote address

# Originally, we used https address when configuring remote warehouse address
git remote add origin / < your GitHub account name > / < your warehouse name >. Git

# Now let's use the Host alias in the config file
git remote add origin<Yours GitHub title of account>/<Your warehouse name>.git

Keep your ssh key in effect

But the reason why I wrote this article is because it's not the first time I've configured it.
For your convenience, please keep looking.
After you restart the computer once, your ssh key will not be managed, and Permission Denied will appear when push ing the code.
Of course, you can repeat the above steps, but we hope to have a more elegant solution. The reason is stated in this article. Those who are interested can learn about it.
Do you have to add SSH add every time

We can add our ssh key to ssh agent again through ssh add command for proxy hosting

ssh-add ~/.ssh/id_rsa_github

However, there will still be the problem of failure after restart, so we can add this command to the command line configuration file.

vim ~/.zshrc

# Add the following, or multiple keys
# Add the ssh key of GitHub to ssh agent
ssh-add ~/.ssh/id_rsa_github
# Then save and exit

Tags: git github ssh

Posted on Tue, 19 Oct 2021 16:10:39 -0400 by sasi