Can't operate GitHub repository using SSH - windows

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

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

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

git commands fail to authorize after ssh authorization succeed to connect to git#github.com

I have created a private git repo on github.
I ran the commands ssh-keygen -t ed25519 -C "me#gmail.com" -f ".\id_ed22519_me" and ssh-add .\id_ed22519_me to create the private and public keys, and added the public ssh key to github.
I also have setup the following config to use the correct keys:
#~/.ssh/config
Host github.com-me
HostName github.com
User git
IdentityFile ~/.ssh/id_ed22519_me
IdentitiesOnly yes
However when I try to use the git command I get the following error:
#powershell
PS C:\Path\to\Project> ssh -T git#github.com
Hi me! You've successfully authenticated, but GitHub does not provide shell access.
PS C:\Path\to\Project> git push --set-upstream origin master
git#github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Can anyone know what could lead to that? I also have made sure that I use the correct url for the remote "origin", and that my branch is indeed "master"
https://gist.github.com/jexchan/2351996#gistcomment-522294
This appears to resolve my issue.
I'm a bit weirded out that I have to append my username to the URL, but it works ^^

Can't clone bitbucket repo locally

I set up my SSH key for my account.
When I run git clone in my terminal I usually get Forbidden fatal: could not read from remote repository.
Running ssh-add -l shows the correct authenticated agent.
Running git clone is now asking for my passphrase with the above error message.
Saying "please make sure you have the correct access rights and the repository exists."
I ran ps to make sure there was only one authenticated agent
The clone command is git clone git#bitbucket.org:<filepath>.git
The URL should:
not be git#bitbucket.org:<filepath>.git
but git#bitbucket.org:<account>/<projectname>.git
you can use:
export GIT_SSH_COMMAND='ssh -Tv'
You will see which key SSH is using.
Make sure the public key has been registered to your Bitbucket account settings.
But the OP adds:
It's private and I'm not the owner. Does something need to be updated with access settings by the owner?
Then yes, your account needs to be granted access in that repository by its owner.

Multiple git account doesn't work with 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

Resources