简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM