This post is in continuation of the original post that will help you understand expected outcome if you decide to add additional git account to your local machine.
I highly suggest you to head over to original post if you are doing this for first time. This post is only helpful for second or successive addition of git account to same local machine.
1. generate SSH key
$ ssh-keygen -t ed25519 -C "your_professional_email@example.com" -f ~/.ssh/<2nd_ssh_key_name>
I named my second key as GitHub_key.
2. add passphrase to SSH key
Running the above command will prompt for adding a passphrase.
$ ls -la ~/.ssh/
total 112
<snip>
-rw------- 1 user staff 484 Feb 29 13:26 GitHub_key
-rw-r--r-- 1 user staff 113 Feb 29 13:26 GitHub_key.pub
<snip>
Public - Private key pair will be generated in the .ssh folder.
3. add SSH key to ssh-agent
$ eval "$(ssh-agent -s)" && \
ssh-add -K ~/.ssh/<2nd_ssh_key_name>
4. setup SSH config file
From the previous post, file named as config is present in the .ssh directory.
There are multiple ways to add the lines of text in this file, however I will provide with two of the easiest ways:
4.1. add text from text editor
I use sublime text as my go to text editor.
$ subl ~/.ssh/config
-
sublfollowed by the path name~/.ssh/configwill open theconfigfile in the sublime text editor.
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/<2nd_ssh_key_name>
Add the above lines of text as it is to the file and save it.
4.2. add text from the terminal
In case you don't know how to open config file in text editor, you can get this on the terminal itself.
$ echo "Host *" > ~/.ssh/config
$ echo " AddKeysToAgent yes" >> ~/.ssh/config
$ echo " UseKeychain yes" >> ~/.ssh/config
$ echo " IdentityFile ~/.ssh/<2nd_ssh_key_name>" >> ~/.ssh/config
Your config file should look like the below snippet
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/<ssh_key_name>
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/<2nd_ssh_key_name>
5. copy SSH public key
You need to copy the contents of the generated public key to add the SSH key to the Git Service Provider account.
$ pbcopy < ~/.ssh/<ssh_key_name>
6. create workspace
Below is the Folder Structure I am using as my workspace to neatly divide the multiple git service provider repos.
/~/
|__.gitconfig
|__/git_repos/
|__GitHub/
|__.gitconfig.GitHub
|__GitLab/
|__.gitconfig.GitLab
You can structure your workspace in any way but remember the path for the additional .gitconfig.<name>. These git configs are going to override the global git config.
7. setup git config
$ git config -f .gitconfig.<name> --add user.email <your_github_email@example.com>
$ git config -f .gitconfig.<name> --add user.name <your_user_name>
$ git config -f .gitconfig.<name> --add github.user <your_github_user_name>
$ git config -f .gitconfig.<name> --add core.sshCommand "ssh -i ~/.ssh/<2nd_ssh_key_name>"
After running all the git config commands your .gitconfig.<name> file should look like the below snippet.
# ~/git_repos/GitHub/.gitconfig.GitHub
[user]
email = <your_github_email@example.com>
name = <your_user_name>
[github]
user = <your_github_name>
[core]
sshCommand = ssh -i ~/.ssh/<2nd_ssh_key_name>
Now you need to specify the additional .gitconfig.<name> file in the Global .gitconfig.
$ git config -f .gitconfig.<name> --add includeIf."gitdir:~/git_repos/GitHub/".path ~/git_repos/GitHub/.gitconfig.GitHub
You Global .gitconfig should now look like the below snippet.
<snip>
[includeIf "gitdir:~/git_repos/GitLab/"]
path = /Users/mac_user/git_repos/GitLab/.gitconfig.GitLab
[includeIf "gitdir:~/git_repos/GitHub/"]
path = /Users/mac_user/git_repos/GitHub/.gitconfig.GitHub
<snip>
I have snipped the output of what Global .gitconfig could look like to avoid confusion as your .gitconfig may have some more config (which is fine).
Top comments (0)