开发者

How do I store a password for my key so I can commit and pull from repository when using git on windows?

开发者 https://www.devze.com 2023-01-14 18:14 出处:网络
Can someone 开发者_如何学运维point me in the direction I need look so I can configure my GIT client with the password needed for my private key? Every time I push and pull from my repository it asks m

Can someone 开发者_如何学运维point me in the direction I need look so I can configure my GIT client with the password needed for my private key? Every time I push and pull from my repository it asks me for the password for my key. I use command line and have the windows GIT client installed to use ssh.

Thanks for any pointers.


I just had this problem here. My local git tree was corrupted, so I deleted it and cloned the project again from github. After that, it started to ask for my password on every pull or push. After some time, I realized I cloned the project with the HTTP URL (https://my_user@github.com/user/project.git). When I cloned again using the SSH URL (git@github.com:user/project.git), it stoped asking for the password.


Are you sure you’re cloning over SSH?

Run git remote -v. The output below is from a repository that clones via HTTPS: notice the https:// URL scheme.

origin  https://github.com/git/git.git (fetch)
origin  https://github.com/git/git.git (push)

Clone URLs for SSH will have one of two forms:

  • username@host.domain.tld:foo/bar/baz.git
  • ssh://username@host.domain.tld/foo/bar/baz.git

Assuming your host supports both SSH and HTTPS (as GitHub, GitLab, and Gitorious do), then simply change the remote’s URL rather than recloning the entire history.

git remote set-url origin git@host.domain.com:foo/bar/baz.git

Different hosts will have different URL designs. For example, switch the above GitHub URL from HTTPS to SSH with

git remote set-url git@github.com:git/git.git

GitHub has a related guide for changing a git remote’s URL.

Windows

On Windows, a typical setup uses PuTTY as the SSH client, which means you’ll want to run Pageant, PuTTY's SSH agent. On Windows, I run a quick batch job out of the Startup group:

@echo off
start /b "C:\Program Files\PuTTY\pageant.exe" "C:\Users\Greg\Greg.ppk"

where Greg.ppk is a key that I created with PuTTYgen.

One more step: tell git to use plink, PuTTY's client for non-interactive connections. Set the environment variable GIT_SSH to

C:\Program Files\PuTTY\plink.exe

assuming that's where PuTTY lives.

Unix, Linux, Cygwin, Git Bash

On Unix-like platforms, the fix is straightforward. If ssh-agent isn't running, start it with, for example

$ eval `ssh-agent`

and then add your default identity with

$ ssh-add

If you have an identity somewhere else, run

$ ssh-add /path/to/other/ssh_id

If you're still having trouble, GitHub has a page for troubleshooting issues with GitHub and SSH, but please also update your question so we can make this a more helpful resource.

Downside of a passwordless SSH key

It may seem tempting to create and register an SSH key that has no password or passphrase because you won’t be prompted for it with each git clone, git fetch, git pull, or git push. However, with no passphrase, your SSH key is unencrypted. This means that anyone who is able to obtain a copy of your private key (the file named id_rsa, id_dsa, id_ecdsa — without the .pub extension) can impersonate you with no effort.

The nice design of SSH means that it is possible to make operations both convenient and secure. Go ahead and make your setup secure today. For a few minutes of extra effort now, you won’t have to wonder about it down the road.

If you’ve already set up a passwordless key, encrypt it by running

ssh-keygen -p

You will see prompts for the key file (with a default value), for your new passphrase, and again to confirm that you typed your passphrase correctly.


You have to recreate your default ssh key and use an empty passphrase. Then upload the public part again to the git server.

Without specific products you use on client and server it is a bit difficult to be more specific.

An alternative is to use ssh-agent, but I have no clue if this also works on Windows or if something similar is available.


The magic words that you are looking for is "ssh-agent", which is a way of automatically entering the passphrase for an ssh key.

This article from Github gives the lowdown on how to get an ssh-agent running using the msysgit Windows client.

Git on Windows is a difficult experience. You will spend your days wandering the internet finding articles that playfully suggest a series of keypresses that only work properly on Unix.

I'm not saying this to taunt you. Just to prepare you for the fact that this is going to be difficult. In many ways, you'll be better off setting up a Unix VM for whatever it is you are trying to do.

That being said, here are a couple of helpful links:

GitHub's Installing Git on Windows series GitHub's Generating SSH Keys on Windows And some general notes about passphrases

If you don't find what you are looking for on Windows, always check the Github docs first. As far as I have been able to tell, they are the only Windows guides worth a damn.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号