- Tell to GitHub what GPG key to use
- Use many users on GitHub and relate them with specific projects
- Create SSH key for every account
- Edit the git local config
- Original git config
- How to clone a public repo using ssh
- Set up different users depending on the directory
Many of the content has been collected from this places:
- https://docs.github.com/en/authentication/managing-commit-signature-verification/generating-a-new-gpg-key
- https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
- https://www.ayyaztech.com/blog/how-to-use-multiple-github-accounts-on-the-same-machine
Check the gpg
version from your current host, some commands will be different between them
gpg --version
generate the key using the gpg
command
gpg --full-generate-key
algorithm: RSA, option 1 size: 4096 expiration: 0, means no expiration
comment: my-comment
passphrase: watch-internally
list the keys:
gpg --list-secret-keys --keyid-format=long
gpg: checking the trustdb
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 2u
[keyboxd]
---------
sec rsa4096/xxx 2024-05-10 [SC]
random.codexxx
uid [ultimate] FrancoGil (my-comment) <email>
ssb rsa4096/yyy 2024-05-10 [E]
go to GitHub, create a new gpg key copy the next output, write the gpg title yoy want:
gpg --armor --export xxx
git config --global --unset gpg.format
# list the keys
gpg --list-secret-keys --keyid-format=long
[keyboxd]
---------
sec rsa4096/xxxyyyzzz 2000-01-01 [SC]
xxxxxx
uid [ultimate] FrancoGil (m1-pro-no-expire-franco-gil) <[email protected]>
ssb rsa4096/zzzyyyxxx 2000-01-01 [E]
...
git config --global user.signingkey xxxyyyzzz
git config --global commit.gpgsign true
new gpg
key, in this case for the "enterprise1" GitHub account
comment: my-comment
pass watch-internally
introduce a current configuration on local in order to define the correct account to use
list the keys
gpg --armor --export xxx
# Remember to add a specific configuration when it is need it, for example when working
# on projects your secondary account. Otherwise the credentials will be globals
# your main account
git config --local --list
git config --local user.name 'my-username or my-real-name'
git config --local user.email my-email
git config --local --unset gpg.format
gpg --list-secret-keys --keyid-format=long
git config --local user.signingkey xxx
git config --local commit.gpgsign true
git config --local core.editor nano
# observe again the local configuration:
git config --local --list
- my-email
- my-another-email
# generate key:
ssh-keygen -t ed25519 -C "my-email"
# the location will have a syntax like this, a postfix at the end:
# /Users/francogil/.ssh/id_ed25519_francogilenterprise1
# add the ssh key to the ssh agent:
eval "$(ssh-agent -s)"
# edit the config
~/.ssh/config
Host github.com.global
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519_francogil
Host github.com.enterprise1
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519_francogilenterprise1
# add your SSH private key to the ssh-agent and store your passphrase
# in the keychain
# ssh-add ~/.ssh/id_ed25519_francogil
# or:
ssh-add --apple-use-keychain ssh-add ~/.ssh/id_ed25519_francogilenterprise1
# Identify added ...
# test the ssh key, will fail, but test it anyway
# you could type the `ssh-agent` command again to reload the git config
eval "$(ssh-agent -s)"
ssh -T [email protected]
ssh -T [email protected]
# add the ssh key to Github
# shh key title:
my-ssh-key-title
# ssh key content, type `cat` and copy the output:
cat ~/.ssh/id_ed25519_francogil.pub
# ssh key type:
# Authentication key
# to save the key, possibly will be required a double authentication key, grab your phone or configure it
# test again your credentials
eval "$(ssh-agent -s)"
ssh -T [email protected]
ssh -T [email protected]
# But do not as expected, return the same account name for both "aliases"
# Hi account-name! You've successfully authenticated, ...
# clone a repository using the expected host:
git clone [email protected]:your_username/your_repository.git
git clone [email protected]:your_username/your_repository.git
See the section: Use many users on GitHub and relate them with specific projects
# introduce a change in the code.
git commit
git push # remember to add your passphrase.
Repeat these steps for every GitHub account to add more accounts!
At least for my current host.
path: /Users/francogil/.ssh/config
Host *.github.com
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519_ssh
Check the current git
configuration:
cat ~/.ssh/config
Host github.com.enterprise1
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519_francogilaenterprise1
Host github.com.global
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519_francogil
Try to log using the suffix desired:
ssh -T [email protected]
Hi realFranco! You've successfully authenticated, but GitHub does not provide shell access.
As we can see the Host
value is different on both configurations, we must
use the suffix .enterprise1
or .global
each time we clone something:
# Before:
[email protected]:docker/compose.git
# After:
[email protected]:docker/compose.git
# ~/.gitconfig
[includeIf "gitdir:~/Documents/DEV/enterprise"]
path = ~/.gitconfig-enterprise
# ~/.gitconfig-enterprise
[user]
name = my_name
email = my_name@aenterprise