简体   繁体   中英

Use Git (Windows) with a private key

I have a Git repository on a Linux server and I want to clone it on Windows.

I have Git for Windows installed and I prefer using it with the command line.

I use SSH with a public key to connect to my Linux server and I have my keys generated and ready to use, but I don't know how to tell git to use the key.

When I just SSH into the box, I use this command:

ssh -i c:\path\to\private\key\id_rsa user@192.168.2.104

So, I guess I'm looking for Git's equivalent of the -i option.

Obviously, this doesn't work:

git -i c:\path\to\private\key\id_rsa clone user@192.168.2.104:/home/user/dev/myproject.git myproject

This is all on my local network.

On Windows, provided %HOME% is set to %USERPROFILE% , use a %HOME%\\.ssh\\config file, which can indicate, for a given entry name, the user and the path of the private key:

Host yourServer
  HostName 192.168.2.104
  User user
  IdentityFile C:/path/to/yourPrivateKey

Test it with ssh -Tv yourServer

Then a clone would be:

git clone yourServer:/home/user/dev/myproject.git myproject

(no need to indicate user@192.168.2.104 or the path to the private key anymore: everything is in the %HOME%/.ssh/config )

That way, you can manage multiple ssh sets of public/private keys.

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