Overview
There are often times when I unintentionally have to work at a company with a personal computer. (I don’t know if it’s the fate of Linux users.) When I first encountered this, I was very uncomfortable, but there were times when Github didn’t apply for multiple accounts, and I have a lot of memories of shoveling because I already run projects and company projects together.
Github operates multiple accounts
It’s been one of the series of why I didn’t apply for it for a long time, but it’s a function that I recently applied for.
https://github.blog/changelog/2023-11-03-multi-account-support-on-github-com/
If you have already registered an account, you can change it through Switch account and you can register additional accounts you are using through Add account. 👍
Local Environment Settings
I checked that the Github account can be changed conveniently by registering it in advance, but how can I work locally? First of all, what I wanted was as follows.
- A and B are projects with different accounts.
- Both A and B want to get a clone of the project using SSH.
- If both A and B commit, I hope the Author information will be left with the appropriate account for the project.
- If both A and B push, I hope they will be pushed according to each account and project.
Github Interworking Settings
Most likely, but in the case of HTTPS, I set it up as SSH because it’s annoying to enter a password every time. However, the problem is that there are two accounts that need to be linked, so you also have to set two keys.
SSH key setting
First of all, you need to set up an additional account, so you need to create a new SSH key and register with a Github account.
1> ssh-keygen -t rsa -C "user-email@gmail.com"
2
3Generating public/private rsa key pair.
4Enter file in which to save the key (/home/penguin/.ssh/id_rsa):
If you type it as above, you’ll get a message asking where you want to save it, and make it the name you want in the lower part of the folder ‘.ssh’ based on the home directory. In my case, I saved it as ‘Myung_rsa’.
When generated as above, a private key and a public key are created, and the public key is paired with the ‘.pub’ extension.
If you made the key like this, register the public key with the github. You have to make it every time like this because it has to be the only place where it is registered.
- [https://github.com/settings/ssh/new ] (access to https://github.com/settings/ssh/new ) (must be logged in and switched to the account you want to register)
- Title writes down the public key that you just generated in the key value so that you can identify it later. (.copy & paste as it is in the pub extension file)
- Click Add SSH key
Github Clone
If you think that since you registered the SSH key, you will have to clone it and pull it, you will get the following error.
1Replicate to 'example-repo'...
2ERROR: Repository not found.
3Fatal: Unable to read from remote repository
4
5Whether you have the right access, and whether you have storage
6Please check it out.
The reason for the above error is that the existing private key is set as the default key for SSH, so the privilege problem arises because the attempt to access the wrong key is made. To solve this problem, it is necessary to make sure that the appropriate private key is accessed during the ssh access.
When you use ssh, you can set which private keys you can use to access a specific address through the ‘config’ file.
- Enter ‘$home/.ssh’ in the folder where the key is stored and create the ‘config’ file.
- Set the value inside the file as follows.
1# Existing configured SSH information
2Host github.com
3HostName github.com
4User git
5IdentityFile ~/.ssh/first_rsa
6
7# About SSH key values you just added
8Host penguin.github.com
9HostName github.com
10User git
11IdentityFile ~/.ssh/second_rsa
To explain the settings above in more detail, it is as follows, but if you have these conditions, you can understand it as a setting that you will use a specific private key.
- **Host penguin.github.com **: This setting configures SSH connections with the alias
penguin.github.com
. - **HostName github.com **: The actual address of the server to connect to is still
github.com
. - User git: The user ‘git’ on GitHub is used.
- IdentityFile ~/.ssh/second_rsa: This connection uses another SSH key,
second_rsa
.
- Try git clone again
1## AS-IS (does not work as a privilege issue)
2git clone git@github.com:penguin-project/example.git
3
4## TO-BE (long motion)
5git clone git@penguin.github.com:penguin-project/example.git
Git Account Separation
If you set it up as above, it’s not too difficult to work on it, but there are still issues about the author when you make a commit. 🤒
If you only use one account, you usually don’t change much, so you’ll usually set it up as below.
1git config --global user.name "penguinimm"
2git config --global user.email "penguinit0619@gmail.com"
Because I use multiple accounts, if I don’t do anything else, I’ll be recorded as an unwanted worker every time I make a commit. To solve this problem, you can set the local settings as below in the corresponding place, or if this is okay each time, you can give different global settings based on the directory.
- Local Settings
- Directory-based Global settings
~/.gitconfig file
1[user]
2name = user
3email = user@gmail.com
4
5[includeIf "gitdir:~/penguin/"]
6path = ~/.gitconfig-penguin
[includeIf "gitdir:~/penguin/"]
: This directive defines how to include additional configuration files under certain conditions, where the condition is gitdir:~/penguin/
, which applies only when working in the ~/penguin/
directory (or its subdirectories)
~/.github-penguin file
Confirm user settings under the penguin directory
Let’s wrap this up
Through this post, you can manage multiple git projects on a specific machine. I hope it will be a post that can help people like me who work on personal projects and work at a company.