Git install and configure SSH Key - Windows

installation

  1. Install Git
    official website to download a Windows version of Git.
  2. Then the next step has been to, the following chart
  3. Environment variable automatic with a good, you can go check the PATH environment variable has no Git environment variables
  4. Then right clicking on the desktop, select Git bash here, and then enter the following codes:
git --version  #验证git是否安装成功,输出版本号就代表安装成功

Configuring global user name and mailbox

There are about global configuration installation is complete the user name and mailbox, enter the following command:

git config --global user.name "自定义用户名"
git config --global user.email "邮箱"

Use the following command to cancel the global settings:

git config --global --unset user.name
git config --global --unset user.email

You can use the following command to view the current global variables:

git config --global --list

SSH Key generation

Manage multiple SSH Key

Problem Description

When there are multiple git account, such as a github, some for their own development activities, again a gitlab, usually within the company git. Both your mailbox if different, will involve a problem, the second generation key git the first time will cover the key, there must be a cause not take.

Solution

We can create a config file to configure it in [~ / .ssh] directory, you can solve the problem

Specific steps:

1. generation

  • Generation of a ssh key (here I use the company's e-mail)
    into the [C: \ Users \ username .ssh], click the right mouse button - select Git bash here, this step is very important, otherwise it below the key name when the key will be generated under the current git bash open the path, for example, I open the desktop git bash, will be generated on the desktop, of course, can be a path completion before entering a name. As shown below:
ssh-keygen -t rsa -C "[email protected]"

When the input file name and path of the generated key named [id_rsa_gitlab], the following consecutive carriage return, the key is not to set a password (see personal needs)

  • The second generation ssh key (here with my GitHub mailbox)
ssh-keygen -t rsa -C "[email protected]"

FIG file is generated as follows:

2. Run the command ssh-agent

ssh-agent就是一个密钥管理器,运行ssh-agent以后,使用ssh-add将私钥交给ssh-agent保管,其他程序需要身份验证的时候可以将验证申请交给ssh-agent来完成整个认证过程

3. 添加私钥,执行下面命令

ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab

在执行上面的添加私钥命令时,如果出现如下错误:

解决方法如下:

  • 输入如下命令查看已开启的ssh-agent线程
ps aux | grep ssh
  • 执行如下命令杀死线程:
kill -9 线程号
  • 进入用户名目录下的.ssh目录,打开git bash,执行如下命令
exec ssh-agent bash
eval ssh-agent -s
  • 再执行如下命令,将私钥放进去
ssh-add ./id_rsa_github
ssh-add ./id_rsa_gitlab

4. 创建并修改config文件

  • 创建config文件,将文件创建在【.ssh】目录下

      ①在windows下新建一个txt文本,然后将名字改成config(包括.txt后缀)
      ②在git bash下,直接touch config即可创建一个config文件
  • 编辑config文件,修改如下内容:

# gitlab
Host gitool.glanway.com
HostName gitool.glanway.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
User mingyue

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
User mingyue

配置文件说明:

每个账号单独配置一个Host,每个Host要取一个别名,每个Host主要配置HostNameIdentityFile两个属性即可
Host的名字可以取为自己喜欢的名字,不过这个会影响git相关命令

例如:

Host mygithub 这样定义的话,命令如下,即git@后面紧跟的名字改为mygithub
git clone git@mygithub:PopFisher/AndroidRotateAnim.git
就相当于你配置的HostName真正的域名,映射成了Host后面的配置的名字

HostName                      #这个是真实的域名地址
IdentityFile                  #这里是id_rsa的地址
PreferredAuthentications      #配置登录时用什么权限认证--可设置publickey,password publickey,keyboard-interactive等
User                          #配置使用用户名

【注意】不要在配置文件中添加下面这样的注释
这种注释在读取该配置文件时会导致报错,不被识别

HostName git.glanway.com //这里填你们公司的git网址即可

生成单个ssh key

输入如下命令,

ssh-keygen -t rsa -C "[email protected]"

生成单个SSH Key时可以连续回车,不输入密钥文件名字和密码:

生成后会在【C:\Users\用户名.ssh】下产生两个文件,如图:

将公钥添加到gitlab或者GitHub上

测试

输入如下代码,见到下图即配置成功

ssh -T [email protected]
ssh -T [email protected]

转载自:https://www.cnblogs.com/Gent-Wang/p/7422433.html

Guess you like

Origin www.cnblogs.com/mingyue5826/p/11141324.html