Multiple git account doesn't work with Xcode - xcode

Due to my customer's privacy requirement, I've got create multiple account to access different git. With some helps, I've created the file config and multiple ssh keys in dir ~/.ssh. Using terminal, I've been able to perform all kind of action to the git. However, if I use XCode to perform those action, it stated that failed to authenticate.
This is the content of ~/.ssh/config:
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_rsa
Host bitbucket-myAccount2
HostName bitbucket.org
User git
IdentityFile ~/.ssh/acc2ssh
Inside ~/.ssh/, I've already had id_rsa, id_rsa.pub, acc2ssh, acc2ssh.pub. id_rsa is my usual git account, and acc2ssh is the other private account.
If I use terminal, it's alright, but it ask for passphrase everytime.
$ git remote -v
origin git#bitbucket-myAccount2:privateTeam/project.git (fetch)
origin git#bitbucket-myAccount2:privateTeam/project.git (push)
$ git pull
Enter passphrase for key '/Users/$myUser/.ssh/acc2ssh':
Already up-to-date.
How do I config this to work in both xcode and terminal?
Additional question: How do I make the terminal to remember my passphrase?
UPDATE
Due to the confusion, I'll add more details about the git remote:
I'm checkout out the git via:
$ git clone git#bitbucket-myAccount2:privateTeam/project.git
Because if I use the ssh provided from bitbucket, it won't work (access denied):
$ git clone git#bitbucket.org:privateTeam/project.git
repository access denied.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
But Xcode seems to doesn't recognize the host bitbucket-myAccount2 as #Jakuje pointed out. Maybe I'm missing something here?

XCode (most probably) does not read your ~/.ssh/config because it is not based on OpenSSH. You should use just the hostname as it is and provide the correct private key.
If the other one (in default location) is still picked up, move it away/rename from default location (~/.ssh/id_rsa) and update the ~/.ssh/config to reflect this change. For example:
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_rsa2

Related

Remote on Git via SSH works differently in the Shell and Xcode

I have set remote using git remote set-url but if it includes username, git works fine on terminal and doesn't work in Xcode. If url was set without username, it works only in Xcode, but doesn't work in terminal.
The error showing in the shell without username is ERROR: Repository not found.
The Xcode error with included username is The remote repository could not be accessed and username in dialog box is git without ability to change
I did not change ~/.ssh/config after OS update. user.name and user.email are set for --local only, no changes here as well.
$ git remote set-url origin git#github.com-UserName:repo/project-name.git
//Remote commands work only in Shell
$ git remote set-url origin git#github.com:repo/project-name.git
//Remote commands work only in Xcode
Where is the problem, how can I fix this issue and get it working in both Xcode and the shell?
PS: Yesterday I had only one of my repos working in both Xcode and shell when set url without username (other repos was working as described only either in terminal or xcode), but after minor Mac OS update (just 12.2->12.2.1) it works the described way also now like others. Have no idea what additionally was set but know it's possible to get working both Xcode and Terminal at least.
UPD: If I change Host from github.com-UserName to github.com I am losing ability to use both repos simultaneously in Terminal because only one repo for added ssh keys works.
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github-User-1
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github-User-2
//Terminal:
$ ssh-add --apple-use-keychain ~/.ssh/UserName-1
$ ssh-add --apple-use-keychain ~/.ssh/UserName-2 //Only repo with this one will work, another will show `ERROR: Repository not found` as both hosts are same.
UPD 2: Looks like Xcode is just ignoring ~/.ssh/config file and just using set remote url as is, not replacing Host name with the actual url.
The URL git#github.com-UserName can only work if you have a ~/.ssh/config file with a Host github.com-UserName entry in it, which reference the right private key to use (the same private key that XCode has in its settings)
Simply rename that Host entry to Host github.com, and the git#github.com:user1/project-name.git should work in command line (as well as with XCode)
If you have more than one repository for the same user, you don't need to add anything to your ~/.ssh/config file.
git#github.com:user1/project-name.git
git#github.com:user1/project2-name.git
git#github.com:user1/project3-name.git
...
If you have more than one user, make sure the Host entry is different than the first in ~/.ssh/config:
Host gh1
Hostname github.com
User git
IdentityFile ~/.ssh/gh1
Host gh2
Hostname github.com
User git
IdentityFile ~/.ssh/gh2
This supposes you have generated two SSH keys, one named gh1, with gh1.pub added to user1, and one named gh2, with gh2.pub added to user2.
ssh-keygen -f ~/.ssh/gh1 -t rsa -P ""
ssh-keygen -f ~/.ssh/gh2 -t rsa -P ""
Finally, use for the repo1 owned by user1
git clone gh1:user1/repo1
# or, for an existing repo1 clone
cd /path/to/local/repo1
git remote set-url origin gh1:user1/repo1
And for repo2, owned by user2
git clone gh2:user1/repo2

Can't operate GitHub repository using SSH

I'm trying certain git operations, such as pull, push etc but none works:
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Things I have done:
Checked whether the remotes were ssh's: git remote -v
origin git#github.com:USERNAME/REPOSITORY.git (fetch)
origin git#github.com:USERNAME/REPOSITORY.git (push)
Checked if the ssh agent is running: eval "$(ssh-agent -s)"
Agent pid 123456
Added the private key to ssh agent: ssh-add "path_to_private_key_file"
Identity added: "path_to_private_key_file" (email#provider.com)
Added the public key to Github at "https://github.com/settings/ssh/new"
Checked whether ssh connection is working: ssh -T git#github.com
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
Assuming that:
you don't have an environment variable GIT_SSH
your %PATH% references C:\Program Files\Git\usr\bin\ssh.exe first
Make sure that:
your remote origin URL is git#github.com:me/myRepo
you have a %USERNAME%\.ssh\config file which set a the right private key file path, assuming it is not the default one (id_rsa).
That config file would be (again, only if the private key is nont the default one):
Host github.com
Hostname github.com
User git
IdentityFile C:\path\to\private\key\file

How to clone more than one private repository on CPanel

I am having some issues cloning a private repo into my CPanel server, because of a previously cloned private GitHub repository. I have cloned it before using ssh access and everything went right. However, I need to clone another repo that is also private and using ssh access, as well.
I followed all the procedures to copy the private repo into CPanel as the previous one. However, I'm having issues with access rights, getting the same message as following:
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Is there a way to clone two private repositories with different ssh keys? how to clone those repositories without getting the same message above?
Thanks in advance.
------------------------------------------------- EDIT-----------------------------------------------------
My config file in the .ssh folder is the following
Host region_admin
HostName <git#github.com:SthefanyRangel/regionadmin.git>
User regioye5#cs2006.webhostbox.net:2083
IdentityFile ~/.ssh/id_rsa
Host renovafacil
HostName <git#github.com>
User regioye5#cs2006.webhostbox.net:2083
IdentityFile ~/.ssh/id_rsa.rf
Host regionadmin-ra2
HostName <git#github.com>
User git
IdentityFile ~/.ssh/region_admin
Where my attempts to connect to the second account are the last record.
After reading the documentation "CPanel / How to Copy an Account with SSH Keys", you might need a second key if the GitLab repository uses the same account than the first one.
If not, try:
git -c core.sshCommand='ssh -Tv' clone git#github:...
That will give you an idea of which keys are considered. If your second key is not with a default name, it would need a ~/.ssh/config in order to specify it.
You can test a config entry with:
ssh -T region_admin
Note: Hostname is always just the hostname, not <git#github.com:SthefanyRangel/regionadmin.git>
Your first ~/.ssh/config entry should therefore be (for the actual GitHub username, for which the public SSH key has been registered):
Host region_admin
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Then:
cd /path/to/regionadmin
git remote set-url origin region_admin:SthefanyRangel/regionadmin.git

Pushing a branch to a remote server fails

I have a project which uses Git on a remote server. I installed SourceTree for Mac to manage it. I also have a .ssh folder with the private in the root directory where I keep all my projects and the public key is in the server.
I was able to clone the project successfully and even commit+push it back with no problem. Then I ceated a branch in my local repo, added a new file and tried commit+push it. It was committed but the pushing failed with the following error message.
Pushing to git#gitserver:gitbtest
ssh: Could not resolve hostname gitserver: nodename nor servname provided, or not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I'm not sure why this is happening. Is there a way to set SSH keys in SourceTree for Mac? I can't seem to find any option to do so.
Any help to resolve this would be great.
Thank you!
An url like git#gitserver:gitbtest can only work if one have an ssh config file in order to resolve the name gitserver:
$HOME/.ssh/config
Host gitserver
Hostname xxx.xxx.xxx.xxx # IP or full name for gitserver
User git
IdentityFile ~/.ssh/id_rsa
Actually, with such a file, you don't even have to specify the user:
git remote set-url origin gitserver:gitbtest
(You can see an example of that ~/.ssh/config file used in "git public key for more repositories")

Permission denied (publickey) errors on Windows when using Moovweb

I'm able to authenticate, generate, push etc just fine with my SSH keys and Moovweb credentials on my Mac and Linux machines.
However, on my Windows machine, using Git Bash, I get an SSH Permission denied (publickey) error. The error message is below:
$> moov generate 123dsfsdsf nytimes.com
Running environment checks.
Verifying that git is installed...OK
Checking that current 123dsfsdsf directory doesn't exist...OK
Registering project with MoovCloud.
Authenticating with MoovCloud.
Checking for git access...Enter passphrase for key '/Users/firstname.lastname/.ssh/id_rsa':
Enter passphrase for key '/Users/firstname.lastname/.ssh/id_rsa':
FAILED
> Need to upload an ssh key in order to generate a project...
Found the following SSH public keys:
1 ) id_rsa.pub
2 ) new_rsa.pub
Which would you like to use with your Moovweb account? 2
Uploading public key...
Successfully uploaded public key new_rsa.pub as 'firstname.lastname#GGT.local'
You are now ready to push projects to MoovCloud!
Creating project in MoovCloud...OK
Generating files...OK
Cloning project locally.
Enter passphrase for key '/Users/firstname.lastname/.ssh/id_rsa':
Enter passphrase for key '/Users/firstname.lastname/.ssh/id_rsa':
Cloning into '123dsfsdsf'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
ERROR: Error cloning git repo: exit status 128
Please try cloning the repository (git clone moov#git.moovweb.com:firstnameglastname/123dsfsdsf.git) again later.
Try 'moov help generate' to find out details.
Seems like a Windows-specific SSH error. Any workarounds?
So as mentioned in prior answers, the Permission denied error in Windows is because you are trying to use a key other than id_rsa.
Windows lacks the bells and whistles that Linux and Mac have to try out all your public keys when trying to connect to a server via SSH. If you're using the ssh command, you can tell it which key to use by passing the -i flag followed by the path to the key to use:
ssh -i ~/.ssh/moovweb_rsa moov#git.moovweb.com
The above command should work just fine if you've uploaded moovweb_rsa.pub to the console (either via the moov login command or the console UI). However, trying any git related commands should fail because Git doesn't give you the ability to chose which key to use when connecting to the git remote. Because of this, SSH is forced to use the default key, id_rsa, and if that key doesn't work (or doesn't exist), then the connection fails with a permission denied error.
One possible solution, as suggested in other answers, is to simply rename your key to id_rsa. For most people, this is a fine solution. However, if you already have an id_rsa key and you would prefer to use a different key with Moovweb, you can edit your ~/.ssh/config file by adding the following contents:
Host git.moovweb.com
IdentityFile ~/.ssh/moovweb_rsa
If you append the above lines to your ~/.ssh/config file (create it if it doesn't exist), you should be able to successfully get Git to communicate with the Moovweb remote git server. The config basically tells SSH that for the given host (git.moovweb.com), SSH should use the given key rather than the default.
It's worth nothing that this happens to all Git remotes; interactions with Github, Heroku, etc... also suffer through this problem in Windows. You could easily extend your ~/.ssh/config file to use separate SSH keys for each one of those services if you so desired:
Host git.moovweb.com
IdentityFile ~/.ssh/moovweb_rsa
Host github.com
IdentityFile ~/.ssh/github_rsa
Host heroku.com
IdentityFile ~/.ssh/heroku_rsa
Quick & dirty solution: use only the default id_rsa.pub key
Some notes:
make sure you enter the right passphrase to id_rsa.pub
do not use your other key, new_rsa.pub
It turns out that Windows Git Bash doesn't quite come with all the cool utilities Mac/Linux users are used to. Specifically, you don't have ssh-agent running to help handle multiple keys. Without ssh-agent, the git command only seems to use the default id_rsa.pub key.
You can verify this is an SSH/Windows issue following Github's awesome SSH troubleshooting guide. You'll get a Permission denied (publickey) no matter which SSH/Git server you try to connect to.

Resources