Skip to content

Instantly share code, notes, and snippets.

@sanjarcode
Last active October 21, 2024 05:30
Show Gist options
  • Save sanjarcode/4d3a2dc172b1f80cf68b32c33157a849 to your computer and use it in GitHub Desktop.
Save sanjarcode/4d3a2dc172b1f80cf68b32c33157a849 to your computer and use it in GitHub Desktop.
Multiple Git accounts on a device, with SSH keys
# ~/.gitconfig - the global config
[user]
name = Muhammad Sanjar Afaq
email = [email protected]
[includeIf "gitdir:~/volopay-work/"]
path = ~/volopay-work/.gitconfig
[core]
editor = code --wait
[alias]
root = rev-parse --show-toplevel
recent = branch --sort=-committerdate
[pull]
rebase = false
[filter "lfs"]
required = true
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
[init]
defaultBranch = main
# ~/.ssh/config contents
# 'HostName' can be anything (kept it this to avoid extra typing)
# personal_user account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/personal_user
IdentitiesOnly yes
AddKeysToAgent yes
# personal_user account (Gist)
# was needed since Gist push wasn't working
Host gist.github.com
HostName gist.github.com
User git
IdentityFile ~/.ssh/personal_user
IdentitiesOnly yes
AddKeysToAgent yes
# work_user - Vimeo
Host github.com-vimeo
HostName github.com
User git
IdentityFile ~/.ssh/work_user
IdentitiesOnly yes
AddKeysToAgent yes
# add more profiles similarly

The idea - avoiding Git interference between accounts

  • Vimeo (company) git creds are used only inside (all levels) ~/vimeo-work folder.
  • All other locations use personal GitHub credentials.

No special syntax needed. Special syntax is needed only at 'clone' time for Vimeo work repos(which are kept in ~/vimeo-work of course)

The setup

All files (real examples) can be found in this Gist.

  1. Create the work folder
  2. Set up the ~/company-work/.gitconfig file.
  3. Set up the ~/.gitconfig file.
  4. Set up the SSH keys
    1. Generate keys on Github.com from personal and work accounts. Make sure to name the keys properly in terminal during key generation.
    2. Set up the ~/.ssh/config file based on keys.

Done!

Cloning vimeo (company) repos.

  1. Go to ~/vimeo-work
  2. Edit the SSH URL. Make it github.com-vimeo instead of github.com. Example: [email protected]:vhx/myRepo.git --> [email protected]:vhx/myRepo.git
  3. That's all.

Pushing/pulling in vimeo/company work

The config for a vimeo repo has the needed credentials. No special syntax is needed.

Optional - Setting up SSH key on a per repo basis (most flexible and easy to do)

The idea:

git config --add --local core.sshCommand 'ssh -i <<<PATH_TO_SSH_KEY>>>'

# this refers to the key file (has no extension), not the .pub file
# FYI, the .pub contents are pasted on GitHub

Why is this way good:

  • This is the easiest since it's easy to add, easy to remove
  • Should be the way if the computer being used is temporary, e.g. like a spare laptop, internet cafe etc.

Exact steps:

# 1. clone the repo
git clone my-repo-url

# 2. go to repo path
cd my-repo

# 3. generate the key pair
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f my_new_key

# 4. add the key to the repo config
git config --add --local core.sshCommand 'ssh -i my_new_key'

# 5. Copy the public key (output)
cat my_new_key.pub
  1. Finally, create a key on GitHub using the copied content. Link: https://github.com/settings/ssh/new
  2. Done!

Note:

  • Make sure to delete the SSH key if computer is not own.
  • If SSH is kept inside repo itself - make sure it's .gitignored.

Source: https://stackoverflow.com/a/62278407

# ~/.ssh directory structure
sanjar$ .ssh tree
.
├── config
├── heroku_play
├── heroku_play.pub
├── known_hosts
├── known_hosts.old
├── personal_user
├── personal_user.pub
├── work_user
└── work_user.pub
0 directories, 9 files
## ~/company-work/.gitconfig
[user]
email = [email protected]

How to work with company GitHub (repos, pull requests) and personal with single vscode.

Setup (assuming)

  1. Vscode is installed
  2. "GitHub Pull Request and Issues" vscode-extension is installed.
  3. Settings sync is authenticated using 'personal' account.
  4. Git configs are globally setup such that commit signing email is correct based on place of repo (/vimeo-work.* vs any other place is personal). So we won't commit with wrong creds.

Repos

vscode is irrelevant here

  • Clone work repos in work folder with the hyper- trick
  • Clone personal repos as usual

Organizations

irrelevant as of now

Issues and pull requests (the main thing)

Type in vscode command palette: 'GitHub Pull Requests: Sign in to Github', and use the respective account (company or personal). All relevant stuff is shown in the extension now. The extension has two actuations (in the side bar):

  1. Browse all PRs and issues (GitHub logo) - open side bar (that I had disabled for long time), expanding will show changes and messages. Clicking on messages will open the file and show the message inside the editor.
  2. For checkout branch's PR (merge branch logo). Changes when you checkout to a different branch.

What about vscode settings and sync?

No change/setup needed. This is independent of PR and issues (company or personal data). I am using personal GitHub for settings sync, which is not relevant to PR or issues.

@sanjarcode
Copy link
Author

Generalizable - change company name and email

@sanjarcode
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment