Git git first-time configuration and how to use the ssh key (the ssh key to the github)

 

 

Configuring the initial installation git user name and mailbox


 

Git initial installation you need to configure the user name and mailbox, otherwise git will prompt: please tell me who you are.

You need to run the commands to configure your user name and E-mail:

$ git config --global user.name "superGG1990"

$ git config --global user.email "[email protected]"

Note :( Please enter the name of your own set of quotation marks, and your own mailbox) user name and mailbox is used to display your identity and contact details of when git commit code, not github username and mailbox

git using ssh keys


 

git git support https and two transport protocols, github share will link two protocols available:

git protocol link Legend: 

https protocol link Legend:

       

git using https protocol, each pull, push will be prompted to enter a password, use git protocol, then use the ssh key, so avoid the trouble of every lost password
 
First-time users want to use git git protocol takes about three steps:
A, generating a key pair
Second, set up a remote repository (github paper, for example) on the public
Third, the git git the remote url modify the protocol (more than two steps after the initial setup too, do not need to set up again after use, this step depending on the remote url future projects may be, if in the future other items you will need to https protocol this step)

 A, generating a key pair


Most will choose Git server using SSH public key for authorization. Each user in the system must provide a public key for authorization, if not necessary to generate a. The process of generating public key are similar across all operating systems. First you have to confirm whether or not the machine has a public key.

~ SSH public default stored in the home directory of the account / .ssh directory. Look inside:

$ cd ~/.ssh
$ ls
authorized_keys2  id_dsa       known_hosts config            id_dsa.pub

Look there id_rsa and id_rsa.pub (or id_dsa and id_dsa.pub like a pair of files), there suffix .pub file is the public key, and the other file is the key.

Without these documents, did not even .ssh directory, you can use ssh-keygen to create. The program on Linux / Mac systems provided by the SSH package, and in the Windows package is included in the MSysGit:

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

Creates a new ssh key using the provided email # Generating public/private rsa key pair.

Enter file in which to save the key (/home/you/.ssh/id_rsa):

Press Enter on the line. Then, you will be prompted to enter a password, as follows (lose a recommended, safer, of course, do not lose the line, should not someone pretending to idle, bored you to modify your code):

Enter same passphrase again: [Type passphrase again]

After over, something like this:

Your public key has been saved in /home/you/.ssh/id_rsa.pub.
The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected]

So far, your local key pair is generated.

Second, add your public key to the remote repository (github)


 

 1. Check your public key generated:

Copy the code
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0X6L1zLL4VHuvGb8aJH3ippTozmReSUzgntvk434aJ/v7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8vR3c8E7CjZN733f5AL8uEYJA+YZevY5UCvEg+umT7PHghKYaJwaCxV7sjYP7Z6V79OMCEAGDNXC26IBMdMgOluQjp6o6j2KAdtRBdCDS/QIU5THQDxJ9lBXjk1fiq9tITo/aXBvjZeD+gH/Apkh/0GbO8VQLiYYmNfqqAHHeXdltORn8N7C9lOa/UW3KM7QdXo6J0GFlBVQeTE/IGqhMS5PMln3 admin@admin-PC
Copy the code

2, login to your github account. Click on your avatar, then Settings -> 左栏点击 SSH and GPG keys -> 点击 New SSH key

3, and then you copy the public key contents above, paste into the "Key" text field. The title field, own just a name.

4. Click Add key.

After completion of this verification key is not working properly:

$ ssh -T [email protected]

Attempts to ssh to github

If you see:

Hi xxx! You've successfully authenticated, but GitHub does not # provide shell access.

Congratulations, you have successfully set up.

Third, modify the git remote url


 

 Use the command git remote -v view your current remote url

$ git remote -v
origin https://github.com/someaccount/someproject.git (fetch)
origin https://github.com/someaccount/someproject.git (push)

If the result is less than the description of this project is to use the https protocol to access (if the address is the beginning of a git git, said the agreement)

You can login to your github, just like the legend of the beginning of this article, you can see the corresponding ssh protocol your url on it, like this:

 

Copy this link ssh, then use the command git remote set-url to adjust your url.

git remote set-url origin [email protected]:someaccount/someproject.git

Then you can reuse the command git remote -v check whether the url has become ssh address.

Then you can happily use git fetch, git pull, git push, no longer have to enter a password annoying

Reference link: http: //www.tuicool.com/articles/BzUrAvF

(Finish)

Configuring the initial installation git user name and mailbox


 

Git initial installation you need to configure the user name and mailbox, otherwise git will prompt: please tell me who you are.

You need to run the commands to configure your user name and E-mail:

$ git config --global user.name "superGG1990"

$ git config --global user.email "[email protected]"

注意:(引号内请输入你自己设置的名字,和你自己的邮箱)此用户名和邮箱是git提交代码时用来显示你身份和联系方式的,并不是github用户名和邮箱

git使用ssh密钥


 

git支持https和git两种传输协议,github分享链接时会有两种协议可选:

git协议链接图例 : 

https协议链接图例:

       

git使用https协议,每次pull, push都会提示要输入密码,使用git协议,然后使用ssh密钥,这样免去每次都输密码的麻烦
 
初次使用git的用户要使用git协议大概需要三个步骤:
一、生成密钥对
二、设置远程仓库(本文以github为例)上的公钥
三、把git的 remote url 修改为git协议(以上两个步骤初次设置过以后,以后使用都不需要再次设置,此步骤视以后项目的remote url而定,如果以后其他项目的协议为https则需要此步骤)

 一、生成密钥对


大多数 Git 服务器都会选择使用 SSH 公钥来进行授权。系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个。生成公钥的过程在所有操作系统上都差不多。首先你要确认一下本机是否已经有一个公钥。

SSH 公钥默认储存在账户的主目录下的 ~/.ssh 目录。进去看看:

$ cd ~/.ssh
$ ls
authorized_keys2  id_dsa       known_hosts config            id_dsa.pub

看一下有没有id_rsa和id_rsa.pub(或者是id_dsa和id_dsa.pub之类成对的文件),有 .pub 后缀的文件就是公钥,另一个文件则是密钥。

假如没有这些文件,甚至连 .ssh 目录都没有,可以用 ssh-keygen 来创建。该程序在 Linux/Mac 系统上由 SSH 包提供,而在 Windows 上则包含在 MSysGit 包里:

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

Creates a new ssh key using the provided email # Generating public/private rsa key pair.

Enter file in which to save the key (/home/you/.ssh/id_rsa):

直接按Enter就行。然后,会提示你输入密码,如下(建议输一个,安全一点,当然不输也行,应该不会有人闲的无聊冒充你去修改你的代码):

Enter same passphrase again: [Type passphrase again]

完了之后,大概是这样:

Your public key has been saved in /home/you/.ssh/id_rsa.pub.
The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected]

到此为止,你本地的密钥对就生成了。

二、添加公钥到你的远程仓库(github)


 

 1、查看你生成的公钥:

Copy the code
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0X6L1zLL4VHuvGb8aJH3ippTozmReSUzgntvk434aJ/v7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8vR3c8E7CjZN733f5AL8uEYJA+YZevY5UCvEg+umT7PHghKYaJwaCxV7sjYP7Z6V79OMCEAGDNXC26IBMdMgOluQjp6o6j2KAdtRBdCDS/QIU5THQDxJ9lBXjk1fiq9tITo/aXBvjZeD+gH/Apkh/0GbO8VQLiYYmNfqqAHHeXdltORn8N7C9lOa/UW3KM7QdXo6J0GFlBVQeTE/IGqhMS5PMln3 admin@admin-PC
Copy the code

2、登陆你的github帐户。点击你的头像,然后 Settings -> 左栏点击 SSH and GPG keys -> 点击 New SSH key

3、然后你复制上面的公钥内容,粘贴进“Key”文本域内。 title域,自己随便起个名字。

4、点击 Add key。

完成以后,验证下这个key是不是正常工作:

$ ssh -T [email protected]

Attempts to ssh to github

如果,看到:

Hi xxx! You've successfully authenticated, but GitHub does not # provide shell access.

恭喜你,你的设置已经成功了。

三、修改git的remote url


 

 使用命令 git remote -v 查看你当前的 remote url

$ git remote -v
origin https://github.com/someaccount/someproject.git (fetch)
origin https://github.com/someaccount/someproject.git (push)

If the result is less than the description of this project is to use the https protocol to access (if the address is the beginning of a git git, said the agreement)

You can login to your github, just like the legend of the beginning of this article, you can see the corresponding ssh protocol your url on it, like this:

 

Copy this link ssh, then use the command git remote set-url to adjust your url.

git remote set-url origin [email protected]:someaccount/someproject.git

Then you can reuse the command git remote -v check whether the url has become ssh address.

Then you can happily use git fetch, git pull, git push, no longer have to enter a password annoying

Reference link: http: //www.tuicool.com/articles/BzUrAvF

(Finish)

Guess you like

Origin www.cnblogs.com/shoshana-kong/p/10994889.html