How can I push git with multiple accounts on one machine - github

I want to use two different github accounts to separate my school and my personal things. So I found the methods here,(https://youtu.be/fnSRBRiQIU8)
I successfully added two ssh keys on each account and this is my ~/.ssh/config file
# Default account
Host github.com
User git
IdentityFile ~/.ssh/id_rsa
# Second account
Host github.com-SECONDARY
User git
IdentityFile ~/.ssh/id_rsa_secondary
I tried to push it but did not have luck.
In the youtube video and its written instruction describe,
1. git remote add origin git#github.com:SECONDARY/testing.git
2. git push -u origin master
I thought it is old way, so I did new way like this
3. git remote add origin https://github.com/SECONDARYusername/testing.git
4. git push -u origin master
Then I got this error message
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Aren't line 1 and 3 equivalent? Is there other method that I can use two accounts on one machine?
Thanks!

The basic technique is to configure SSH with two new virtual (ie. fake) host names. They both point at github.com, but one uses one key and the other use the other. Your ssh config has a problem, it doesn't specify what the real host is.
# Second account
Host github.com-SECONDARY
User git
IdentityFile ~/.ssh/id_rsa_secondary
That says "when you try to connect to github.com-SECONDARY, use the ssh key in ~/.ssh/id_rsa_secondary". But github.com-SECONDARY isn't real. You need to tell ssh that by adding a HostName line.
# Second account
Host github.com-SECONDARY
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_secondary
That's the first problem.
The second is you're not using that hostname in your remotes.
git remote add origin git#github.com:SECONDARY/testing.git
^^^^^^^^^^
That's the hostname part. It should be github.com-SECONDARY like so.
git remote add origin git#github.com-SECONDARY:SECONDARY/testing.git
Then ssh will know to use your special config for the github.com-SECONDARY virtual host.
There's a better info on this in the Q&A for "Multiple GitHub Accounts & SSH Config".

Related

Trying to setup dual repo SSH config file

I've successfully had my MacBook working with my own Github repo for years. Recently, I tried to setup my computer to access both my own Github repo and the Github repo for my work account. I read many articles and threads and learned that this is supposed work with the SSH config file. I've tried many things but just can't get it to work.
In my .ssh directory I have these files:
config
id_ed25519
id_ed25519.pub
id.rsa
id_rsa.pub
The first private/public key is for my work account and the second is for my personal account.
Here's the config file I [naively] created:
Host https://github.com/employer
HostName github.com
IdentitiesOnly yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519
Host RobertWerner-MWTech.github.com
HostName github.com
IdentitiesOnly yes
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
I've obscured the name of my employer but that http://... address is what my DevOps colleague told me to add. For my personal account, the first part - RobertWerner-MWTech is my Github account name. I don't know if RobertWerner-MWTech.github.com is the correct host name.
Anyhow, it seems that the config file has no actual effect. Even with it in place, I can't access both repos at the same time. The best I can achieve now is to run ssh-agent whenever I want to use a specific repo, like this:
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
OR
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_rsa
While this does allow me to switch back & forth between the two repos, it would be fantastic if I could actually get the config file to work as it's supposed to.
Any guidance on how to resolve this would be much appreciated!
Robert

Messed up with deploy key

When I try to push my files to the git server I faced this problem.
`~ git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
So, I've entered git push --set-upstream origin main in the terminal. It was showing up the following error.
ERROR: Permission to adivenkat05/C-Assignments.git denied to deploy key
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
So, after searching for the solution, it was that we need to upload our ssh key in the git server (Deploy Key).
I've uploaded that same key to another repository but it ended up showing me this "So, after searching for the solution, it was that we need to upload our ssh key in the git server (Deploy Key).
I've uploaded that same key to another repository but it ended up showing me this "Key is already in use".
So, it's not possible to have multiple ssh keys in a particular machine, right?
How do I tackle this problem?".
The second error is different, and refer to an authentication problem.
You can have multiple keys, a deploy one and one associated with your user account.
Create a new dedicated key, and add it to your account:
ssh-keygen -t rsa -P "" -f ~/.ssh/me
Reference it in a ~/.ssh/config file:
Host gh
Hostname github.com
User git
IdentityFile ~/.ssh/me
IdentitiesOnly yes
And your SSH URL becomes gh:me/myRepository. (no more git#github.com:...)
You can test your SSH key with ssh -Tv gh.
It it works:
cd /path/to/my/local/repo
git remote set-url origin gh:adivenkat05/C-Assignments.git
git push -u origin main

GitHub using the wrong SSH key

I have two GitHub accounts setup with ssh keys, one personal and one enterprise.
I have an ssh config file as such:
# * GitHub CKO SSH Key
Host github-enterprise
HostName github.com
AddKeysToAgent yes
UseKeychain yes
User git
IdentityFile ~/.ssh/id_ed25519_github
# * GitHub Personal SSH Key
Host github-personal
HostName github.com
AddKeysToAgent yes
UseKeychain yes
User git
IdentityFile ~/.ssh/gh_mervinhemaraju_ed25519
Both keys were created seperately and attached to the respective account.
The weird issue is that I was using this for like a month, and it was working. Today, when i logged in, i committed some work on my personal repo and when i tried to do a remote push (which was working for this repo previously), i got and user permission denied.
I then performed an ssh test on both ssh keys and the results was as such:
ssh -T ssh -T git#github-personal
Hi mervin-hemaraju-enterprise! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git#github-enterprise
Hi mervin-hemaraju-cko! You've successfully authenticated, but GitHub does not provide shell access.
The personal key test is wrong. It should've been Hi mervinhemaraju! You've successfully authenticated, but GitHub does not provide shell access. since mervinhemaraju is my personal account, but is instead referring to the enterprise one.
I am on MacOs. Can someone please help ?
Add IdentitiesOnly yes for the entries in the ssh config. This will prevent the SSH Agent from trying all the keys it knows of and only use the ones specified in the config file.
Specifies that ssh should only use the identity keys configured in the ssh_config files, even if ssh-agent offers more identities.
https://www.ssh.com/academy/ssh/config
I have had a similar issue, and what I did was create a local git config that explicitly specifies which SSH key to use.
My ~/.ssh/config file specifies to use my "work" ssh key, since that's most common on my work computer.
I have something like this in my ~/.ssh/config:
Host *
PreferredAuthentications publickey
IdentityFile ~/.ssh/my-work-ssh-key
ServerAliveInterval 60
In my "personal" projects, I configure Git to ignore my ~/.ssh/config file, and I set the identity file to my "personal" SSH key.
Something like this:
git config --local core.sshCommand "ssh -i ~/.ssh/my-personal-ssh-key -F /dev/null"
That puts the following in .git/config:
[core]
sshCommand = ssh -i ~/.ssh/my-personal-key -F /dev/null

Github: I have to make a new SSH key after every use

I have a school and a personal Github, so I made an SSH key for my personal account and linked it up. It always works only once, then gives me
Push Failed
Git#github.com: Permission denied (publickey). Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
It works fine again after I delete the old SSH and make a new one, but this is obviously a huge hassle. I am on Windows 10, using Git Bash, and IDE is IntelliJ.
Any ideas? Thanks.
I solved this problem in the following way:
1.Create a file named config in the path $USER_HOME/.ssh/, then add following content.
# For school account
Host school_github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_school
# For personal account
Host personal_github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_personal
2.Run following command using git bash:
Change your school github repository configuration
git remote rm origin
git remote add origin git#school_github:your_github_username/your_repo_name.git
You can hava a try.
You don't need to create SSH keys for each git service just upload the same public key to both personal and school accounts and it will work.
SSH public key is tied to you local account you have generate SSH keys only if you are using different local(machine) account or different machine
Seems to me like when you try to authenticate, you are probably not using your keys.
If you run the command
ssh-add -l
You (should) see a list of all your keys. Each time you start a new session, make sure to start up the agent and add your keys.
eval `ssh-agent`
ssh-add ~/.ssh/id_rsa

How to use multiple Git SSH keys on Eclipse?

I looked several answers and forums for a solution but I could not find a single one that works.
I have this scenario:
Eclipse Luna Service Release 2 (4.4.2)
Ubuntu 14.04 x64
Two ssh keys on my ~/.ssh folder
Two bitbucket accounts (one for personal projects and one for enterprise)
A git repository only accessible with my primary key (~/.ssh/id_rsa)
A git repository only accessible with my secondary key (~/.ssh/other)
I created a ~/.ssh/config file with the contents:
Host bitbucket bitbucket.org
Hostname bitbucket.org
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/other
User git
And for the sake of sanity I added the second key using ssh-add as well. Running ssh-add -l lists both keys.
When using the command line, all git commands work like a charm, with both repositories. But when using Eclipse, I always get the Invalid remote: origin error when trying to clone or pull from the repository with the secondary key:
Caused by: org.eclipse.jgit.errors.NoRemoteRepositoryException: git#bitbucket.org:myuser/myrepository.git: conq: repository access denied.
I added the secondary key at Window > Preferences > Network Connections > SSH2 > Private keys, and set the GIT_SSH environment variable to point to my ssh executable:
$echo $GIT_SSH
/usr/bin/ssh
I've restarted Eclipse and even the OS several times, with no luck.
Since I can use git from the command line without problems, I tend to believe there's something wrong with Eclipse.
How to use multiple Git SSH keys on Eclipse? Or how to force Eclipse to use my secondary key on a single project?
Host bitbucket bitbucket.org? You don't declare multiple entry names on one Host section.
I would expect to see in a ssh config file declaring multiple keys:
Host bitbucketuserA
Hostname bitbucket.org
IdentityFile ~/.ssh/id_rsa
User git
Host bitbucketuserB
Hostname bitbucket.org
IdentityFile ~/.ssh/other
User git
And you would use ssh url like
bitbucketuserA:userA/myrepo1
bitbucketuserB:userB/myrepo2
(this is similar to what I suggested for "How to work on personal GitHub repo from office computer whose SSH key is already added to a work related GitHub account?")