Skip to content

Instantly share code, notes, and snippets.

@realFranco
Created May 10, 2024 23:06
Show Gist options
  • Save realFranco/eafd546db3ed3ca3dec11d082251ac69 to your computer and use it in GitHub Desktop.
Save realFranco/eafd546db3ed3ca3dec11d082251ac69 to your computer and use it in GitHub Desktop.
Guide to follow when it is required more than one GitHub account and make commits on distinct contexts.

Github credentials

Index

Many of the content has been collected from this places:

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

Tell to GitHub what GPG key to use

https://docs.github.com/en/authentication/managing-commit-signature-verification/telling-git-about-your-signing-key#telling-git-about-your-gpg-key

git config --global --unset gpg.format

# list the keys

gpg --list-secret-keys --keyid-format=long

git config --global user.signingkey xxx

git config --global commit.gpgsign true

Use many users on GitHub and relate them with specific projects

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

Create SSH key for every account

  • 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

Edit the git local config

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!

Original git config

At least for my current host.

path: /Users/francogil/.ssh/config

Host *.github.com
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_ed25519_ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment