Use multiple Git SSH identities on a single computer
github.com
github.com
[includeIf "gitdir:~/work/client1/"]
path = ~/work/client1/.gitconfig
[includeIf "gitdir:~/work/client2/"]
path = ~/work/client2/.gitconfig [includeIf "hasconfig:remote.*.url:https://client1domain.com/**"]
path = ~/work/client1/.gitconfig
[includeIf "hasconfig:remote.*.url:https://client2domain.com/**"]
path = ~/work/client2/.gitconfig
Check comment [2] for a complete example with pinning SSH key and signing key using git-config.[1] https://git-scm.com/docs/git-config#Documentation/git-config...
I create a top level directory for collection of related repositories. It lets me configure git for different collection of repositories.
Host github_as_pulp
Hostname github.com
User git
IdentityFile ~/.ssh/pulp.../id_rsa
IdentitiesOnly yes
I just need to replace github.com by github_as_pulp on my clone command to use my second github account. As it remains in .git/config, it matches automatically when I cd between repositories.In another script, I have GIT_SSH_COMMAND="ssh -i /.../.ssh/id_rsa -F /dev/null" git archive --remote=git@bor...fr:ace/docsite.git master | tar xp docsiteapp
git and ssh are so versatile. I think that a little documentation is better than a new tool.
Too many people don't enjoy reading the manpages.
I have multiple SSH keys on my computer filling different roles (work, personal, niche hobby stuff). I set them up like you did, and then never think about it again.
But... I guess if you didn't want to type this and were okay with an additional dependency, gitid has the advantage that you just have to remember what identity is current.
Since it's unclear i'm not sure how this is useful.
Also you can configure the identity to use in ssh per host using aliases. If you want to switch identities globally, just load the appropriate key in ssh-agent.
here is an ssh config which binds a specific identity to personal.github.com
Host personal.github.com
ControlMaster no
Hostname github.com
IdentityFile ~/secret/keys/personal/github
IdentitiesOnly yes
bonus points if you set IdentitiesOnly=yes globally [includeIf "gitdir:~/work/client1/"]
path = ~/work/client1/.gitconfig
[includeIf "gitdir:~/work/client2/"]
path = ~/work/client2/.gitconfig
$ cat ~/work/client1/.gitconfig
[user]
email = emailforclient1
name = nameforclient1
signingKey = 3AA5C34371567BD2
[core]
sshCommand = ssh -i ~/.ssh/id_client1
$ cat ~/work/client2/.gitconfig
[user]
email = emailforclient2
name = nameforclient2
signingKey = 4BB6D45482678BE3
[core]
sshCommand = ssh -i ~/.ssh/id_client2
See:- https://news.ycombinator.com/item?id=36798978
- https://git-scm.com/docs/git-config#Documentation/git-config...
Host *.github.com
IdentityFile ~/.ssh/%h_id_rsa
IdentitiesOnly yes
HostKeyAlias github.com
ProxyCommand nc github.com 22
The `ProxyCommand` hack makes a global `ControlMaster auto` still work with a `%C` based `ControlPath`, but using `Hostname github.com` should work when using something like `%n_%p_%r`.Please don’t write command line tools in javascript. I’d never install npm, yarn… on my system just to try a tool out. CLI tools are perfect for compiled languages so you can distribute a binary.
This package in question has no dependencies and I can read the source code, can't do that with a binary.
Though I agree, but for other reasons, this tool is a bit silly in JS because you also need node, and it's basically just moving files around.
The issue is fragmenting tools between multiple package managers. That's a pain, and the source of my opposition to this concept here. Doubly so if they're language specific package managers (fine for development/in isolation, not for system wide user facing tools though).
Not really as npm and yarn are whole package managers that work in parallel to your distro's standard package manager. They pull various sources (that haven't been vetted by anyone except hopefully by the end programmer who told you to use npm install) from who knows where and install them either in your own home, or system-wide with -g like suggested here.
I would never use npm to install a system tool.
It looks like InderdeepBajwa, specifically, has tried to make a couple of projects in various languages: php, c++, go, python, javascript. So, the refrain 'write what you know' does not quite apply.
This is such a rude thing to say to someone providing free software. If you want to write tools in the language of your choice, then you're free to do so.
See how ridiculous it sounds now?
people are way more likely to be setup with npm than they are with cargo
cd <git repo>
git config --local core.sshCommand "ssh -i ~/.ssh/id_<private key to use>"
Job done.https://git-scm.com/docs/git#Documentation/git.txt-codeGITSS...
Created a seamless solution for them. It lets you: - create and switch between work and personal SSH accounts in any repository - create new git SSH keys, and switch within existing git repos - upcoming: automatically tie git config user{name, email} to an SSH identity so that your commits follow your identity
Note: It supports Github.com at the moment. Support for any other git provider coming quick!
In the end, I just gave up and forked over some cash for GitKraken, where it's as easy as clicking on a dropdown, in addition to also being cross platform and faster to work with than something like SourceTree. They even have a complete documentation page that describes how to do this exact thing, and it works the same across multiple platforms (Windows and Linux in my case): https://www.gitkraken.com/blog/managing-mulitple-github-acco...
Sometimes paying someone who has solved your exact problem pretty well is the path of least resistance, versus trying to figure out everything yourself. That's more or less why I'm also paying for the JetBrains IDEs, even if most of the software I use is free/open source. Nothing wrong with various scripts and utilities, though!
This is basically only a problem with Azure DevOps, which has a completely broken SSH implementation. It accepts any key, and only later checks permissions. Other source forges do not have this issue, as they only accept keys that have access.
So basically, this might be convenient if you use Azure DevOps, but otherwise there should be no need whatsoever.
If there is a need to use separate identities, without trying to be pedantic, simplest means is via understanding the ssh tooling in POSIX compliant systems. Thinking of solving this everyday operational problem with JS/TS packaging is inventing a new operational problem. NPM might be great for desktop & web application dev, but I wouldn't consider the stability as rock solid - something I will trust my system tasks with.
alias workgit="git config user.name 'full name' && git config user.email 'karan@org.tld'"
alias persgit="git config user.name 'full name' && git config user.email 'karan@my.email'"
I prefer this because there are some overlapping directories in my filesystem and don't want to automatically use another git identity just based on file paths. Works well enough for my usecase. programs.ssh = {
enable = true;
forwardAgent = true;
extraConfig = ''
Host server-one
HostName 123.0.0.1
Port 22
User root
IdentityFile ~/.ssh/some_rsa
Host server-two
HostName 123.0.0.2
Port 22
User root
IdentityFile ~/.ssh/other_rsa
'';
};I currently use KeepassXC to manage my ssh keys, but then get tripped up with the multiple identities sometimes. I have separate keys for personal vs work on github/ gitlab, and when I attempt to perform git push/ pulls, I often have the wrong one in my ssh-agent's keyring.
You can also avoid setting the global settings and do it for each repository which depending on how many you have might not be much of an issue.
disclaimer: Written by a friend of mine.
I may try the one you linked....
[0]: https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-...