Why is git asking me for a password? - windows

I'm using Windows XP, TortoiseGit (latest version) and Cygwin. I have git.exe installed and on my path. In TortoiseGit, I can execute pushes against the remote repository without being prompted for a password. I assumed this was because I have defined this in my .git/config file ...
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = ssh://git#myrepo/myproject.git
puttykeyfile = U:\\.ssh\\mykey.ppk
However, in Cygwin, when at the root directory of my project (the one where the .git folder is a child), I get prompted for a password ...
$ git push origin qa_release
git#myrepo's password:
Why am I prompted for a password when trying to push from Cygwin but not from TortoiseGit and how can I get Cygwin to behave like TortoiseGit (i.e. not ask me for a password)?
Thanks, - Dave

puttykeyfile is a windows git thing, so you need to have your SSH set up for cygwin too. Git will use your regular SSH settings.
Is the .ppk file a regular SSH public key? If so, add the following to your ~/.ssh/config file, so cygwin git knows where to find it.
Host myrepo
IdentityFile /cygdrive/u/.ssh/mykey.ppk
You can test this quickly with a ssh myrepo in cygwin.

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 automatically use a specific SSH key when using Git through a JetBrains IDE (i.e. PyCharm) on Windows?

I would like to use a specific SSH key when using Git through PyCharm on Windows.
Things I have tried:
Tried configuring SSH by adjusting the ~/.ssh/config (and permissions) as mentioned in these answers:
https://stackoverflow.com/a/31044770/8488985
https://stackoverflow.com/a/11251797
https://superuser.com/a/232406 (See for comments about permissions)
This enabled Git when used in the Git Bash command line, however, it did not work when using the IDE.
Tried adding the SSH key to the ssh-agent as mentioned in here:
https://stackoverflow.com/a/4565746
Again, this enabled Git when used in the Git Bash command line, however, it did not work when using the IDE.
What else can I do?
Apparantly JetBrains IDEs (like PyCharm) use the Windows commandline to interact with git.
See here
So the first step is to startup the Windows Command Prompt and check if you can use Git with a specific SSH key that way.
In my case this was not possible, even though I had configured the config file located at ~/.ssh/config (when accessed through Git Windows) or C:\Users\<Me>\.ssh\config.
This is why the SO posts mentioned in my question did not solve my problem.
The second step is to figure out which config file instead is used by Git when accessed through the Windows Command Prompt.
This provided me the clue that Git on Windows can use an internal ssh config file called ssh_config (instead of config) and located near the git.exe (instead of in a subdir of your homedir).
I found Git's location using where git which indicated C:\Program Files\Git-2.16.1\cmd\git.exe.
So the config I was looking for was located at C:\Program Files\Git-2.16.1\etc\ssh\ssh_config.
The third step is adjusting that ssh_config file similar to the config file.
For me this looked like this:
Host <some-name>
HostName gitlab.com
IdentityFile C:\Users\<Me>\.ssh\<my-non-default-key>
# Prevents usage of the default IdentityFile
# See: https://stackoverflow.com/a/11251797
IdentitiesOnly yes
# Prevents warning when pulling from Gitlab.
# See: https://stackoverflow.com/a/67556915
UpdateHostKeys no
The fourth and last step is to change the remote in the Git repository such that it uses the name <some-name> specified in the ssh_config file.
Get the original remote first using git remote -v.
This gives something like this:
git#gitlab.com:some/subdirs/to/the/repository.git
Now swap out the gitlab.com part for <some-name> specified in the ssh_config file, like this:
git remote set-url origin git#<some-name>:some/subdirs/to/the/repository.git
Summary:
When using a JetBrains IDE like PyCharm it uses Git over the Windows Command Prompt
Find out the location of Git in the Windows Command Prompt
Adjust the ssh_config file located at this Git location
Set the remote url of the repository such that it uses the host name specified in the ssh_config file

Can't clone, can SSH. "Permission denied (publickey)."

I cannot clone or push to a repository on my server.
I have a bare repo that is located is a directory user#host in directory home/user/test.git that I am trying to access via git clone. I used ssh-add <pathtokey> to add my ssh key. It asked me for the passphrase. I can then ssh user#host successfully.
However if I then try to git clone ssh://user#host/~/test.git I get:
Cloning into 'test'...
user#host: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
also tried
git clone ssh://user#host/home/user/test.git
git clone user#host:home/user/test.git
git clone user#host:/home/user/test.git
with the same result
I am guessing the git credential manager isn't picking up the keys?
On the server /var/auth/log says
Feb 20 02:25:36 xxxxx sshd[24674]: Connection closed by authenticating user XXXX x.x.x.x port 56433 [preauth]
Git version: git version 2.30.1.windows.1
Git Credential Manager: Git Credential Manager version 2.0.318-beta+44acfafa98 (Windows, .NET Framework 4.0.30319.42000)
git config -l reports credential.helper=manager-core
Tried both PowerShell and git bash shells, same result
user has read, execute permissions to the repo
To add to #VonC's response
In git-bash things work as normal.
The normal flow of starting the ssh-agent (via eval 'ssh-agent'
), adding the key via ssh-add <path_to_key> enables git clone to work.
In PowerShell Core, or Cmd, via Windows-Terminal more work is required
the ssh-agent starts automatically (assuming you previously started the OpenSSH Authentication Agent service), adding the key works, and you can ssh after this, but git commands do NOT work, initially, but if you do
git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe
This will replace the (default) ssh that comes with git-for-windows with the Windows10 implementation.
After this it should work fine in things other than git-bash. Confirmed in Powershell-Core, Command Prompt
See also:
https://gist.github.com/danieldogeanu/16c61e9b80345c5837b9e5045a701c99
The git credential manager is only involved for caching credentials (username/password) for HTTPS URL, not SSH.
Only the ssh-agent could be involved, for caching a possible passphrase, if the private key was defined with it.
I would try first using the full path, since ~ might not be interpreted by the remote shell, but the local (which has a different path for ~):
git clone ssh://user#host/home/user/test.git
# or
git clone user#host:/home/user/test.git
If not, in a git bash session, type:
export GIT_SSH_COMMAND='ssh -v'
git clone ...
The OP confirms in the discussion it works in a bash session:
In git bash, I started the ssh-agent,
added the key there, then it worked.

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 and SSH - How to know what address to use?

I need to set up a git repository in the production server so I can upload all my commits. So, these are the steps I followed:
Production Server (Windows Server 2008)
Installed Git
Created an empty repository at inetpub/wwwroot
Installed CopSSH
Created a new user account
Enabled the user with CopSSH
Changed the default directory of the new user to the repository address
Setted environment variables to read Git commands
Development (Windows 7 + Netbeans)
Installed Git
Created a repository right where my project is
Added all files of the project to the repository
Now, I need to upload my dev project to prod. First, I'm checking if the SSH default directory where I'm pointing to is a repository:
$ git ls-remote ssh://user#server/
fatal: '/' 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.
I tried adding the folder name, ".git", foldername.git, everything, and I get the same error.
Where am I having the issue? Thank you beforehand.
P.S. I already tested the new default directory and it points right to the empty repository in the server, so it's working.
EDIT: I tried this:
$ git ls-remote user#host:.git
user#host's password:
$_
I got no output. Is that okay?
You're saying the git repository at your server is located at / (say, you should have a /.git directory). As that's probably not true, I'd suggest you to connect via ssh to the server and run pwd (or it's Windows equivalent) to found where your home directory is, and then get the relative path to your repository.
Say, if running pwd right after logging into the server gives you C:\users\my_user and your git repository is at C:\users\my_user\git\my_project, use ssh://user#server:git/my_project as the URL. The : means a relative path from your home directory.
Also you can use an absolute path, as in ssh://user#server/c/users/my_user/git/my_project, or however Windows decides to name your C: partition :)
As you use ssh transport, your syntax tries to access the root of your Copssh installation, Example: if you installed Copssh at c:\copssh, then your root (/) is the same directory.
Your command git ls-remote ssh://user#server/ tries to access that directory.
Try something like
git ls-remote ssh://user#server/home/user* (c:\copssh\home\user) or
git ls-remote ssh://user#server/cygdrive/d/mygit if your repository is at d:\mygit)
If you all want to create a Windows based Git repository, you may consider to stick with Gitwin which has a fully functional free edition. It is Git on Cygwin though and Disclaimer: I'm the developer.

Resources