Installing golang package from private gerrit repository - go

I'm trying to use one of projects hosted in gerrit as a dependency in golang.
I installed go-import plugin in gerrit
I added this to .gitconfig:
[url "ssh://myuser#gerrit.example.com:29418"]
insteadOf = https://gerrit.example.com
Now I run:
go get gerrit.example.com/myproject
but it fails with:
go get: module gerrit.example.com/myproject: git ls-remote -q origin in /home/me/go/pkg/mod/cache/vcs/513f491cb527a8cec5b684e8d77254c851f76499e1f725440f98d4e9ad8bbf4f: exit status 128:
fatal: project a/myproject not found
fatal: Could not read from remote repository.

Make sure that global git configuration is not setup with something along the lines:
git config --global url."git#gerrit.example.com:".insteadOf "https://gerrit.example.com"
.gitconfig
[url "git#gerrit.example.com"]
insteadOf = https://gerrit.example.com
For regular use of the git tool, it's convient to have a ~/.ssh/config file set up. In order to do this, run the following commands:
mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config
Please note the permissions on the files and directory above are essentail for SSH to work in it's default configuration on most Linux systems.
Then, edit the ~/.ssh/config file to match the following:
Host gerrit.example.com
HostName gerrit.example.com
Port 29418
User USERNAME_HERE
IdentityFile ~/.ssh/id_gerrit_example_com
Please note the spacing in the above file matters and will invalidate the file if it is incorrect.
Where USERNAME_HERE is your gerrit username and ~/.ssh/id_rsa is the path to your SSH private key in your file system. You've already uploaded its public key to gerrit.
Note: I have created this configuration just on assumption since i do not have gerrit setup to test on.

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

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 ^^

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

Git remote add origin: correct URL and path to add remote repo on the local network

I am trying to set up a remote repository on a Mac on my local network. I have done the following:
Set the sharing privileges on the remote, allowing access to all users (using System Preferences/Sharing)
confirmed that I can SSH to the remote machine
created the repo in the remote directory
created a repo in the local machine directory
executed this command from the local repo directory:
git remote add origin FSM13#192.168.1.51:/Library/FileMaker\ Server/HTTPServer/htdocs/fm-php
when I attempt to push:
git push -u remote origin master
I get these errors:
fatal: 'remote' does not appear to be a git repository
fatal: Could not read from remote repository.
I get the same error using this syntax to set the remote
git remote add origin ssh://FSM13#192.168.1.51/Library/FileMaker\ Server/HTTPServer/htdocs/fm-php
FMS13 is the administrator user account on the remote machine.
What steps might I be missing?
There were three things I needed to do in order to solve this issue:
set up the SSH Keys correctly
initialize the remote repo correctly (using --bare) (this worked but I need to try a few more things. a bare repo does not have any files, just the history: http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/ )
set the path correctly for the git remote add command
Note that I am using OS X.
This youtube vid and blog post laid it out perfectly:
https://www.youtube.com/watch?v=DDzeiI2yZL8
http://crosbymichael.com/setup-password-less-login-over-ssh.html
SSH Keys:
On the local machine, starting from home directory:
cd .ssh
cat id_rsa.pub
then copy the printed public key. I happened to already have one. The blog post above explains how to create one.
SSH into the remote:
ssh FMS13#192.168.1.51
ls -a
Is there a .ssh directory? If not create one. It needs to be in the home directory.
mkdir .ssh
cd .ssh
then create a keys file
touch authorized_keys
nano authorized_keys
Then paste the key and save the file.
Confirm the install of the key by ssh'ing from the local machine:
ssh FMS13#192.168.1.51
No password was requested, so the key install was successful
Correct repo initialization:
On the remote machine, cd to the desired parent directory and:
mkdir remote-git
cd remote-git
git init --bare
On the local machine from the repo directory, the correct path was as follows to set the remote origin:
git remote add origin ssh://FMS13#192.168.1.51/Users/FMS13/Desktop/remote-git
Where FMS13 is the user on the remote computer, followed by the ip address and then the full path to the directory of the remote repo.
Push to remote:
Then back in the local machine, cd to the repo directory and:
git push origin --all
I have used something similar to this command recently which worked for me:
git remote set-url origin ssh://FSM13#192.168.1.51/USERNAME/REPOSITORY.git
Check out this link for more information. For your case, you may need to be adding the following instead. I have not seen using an IP address with this, though- so refer to the link.
ssh://git#192.168.1.51/USERNAME/REPOSITORY.git
You may also need to generate a new ssh key. Here is a link on that:
ssh-keygen -t rsa -C "your_email#example.com"

Resources