Use Git quickly connect to GitHub

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/swadian2008/article/details/94996390

table of Contents

First, the New Connection

1. Create a github repository

2, created locally ssh key (git)

3, the configuration keys (GitHub)

4, test connection (Git)

5, upload the code (git)

(1) initialize a local git repository

(2) attempt to connect and code uploaded

Second, the common error

1, there is no match to any resources

2, git can not find a local warehouse

3, did not submit the authorization


First, the New Connection

1. Create a github repository

If you have not github account, sign up for github account, the process is too simple, not detailed here.

After github remote repository created operations as shown above, click to create a warehouse, there will be the following interface, the operation at this time to come to an end on the github

2, created locally ssh key (git)

Here you need to install git, git detailed installation guide can refer to this article: https://www.jianshu.com/p/a152f82c5e4a

This article demonstrated here using the Windows version, open git, below the input command to generate keys

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

Figure appears next prompt, indicating that the local key generation success.

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Created directory '/c/Users/Administrator/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:/uxaNgC1gXzFSQFugBg7RzLTSoBbrl2z0iZVBaCbTvM [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|o.=+o+o+==+o     |
|. +Bo ++.oo      |
| =+.....+        |
|. o=+  o         |
| o=+ o  S        |
|.o+o+  . .       |
|  .+E   . +      |
|         = .     |
|        .o+      |
+----[SHA256]-----+

As shown, the clip swells in the local computer file .ssh become a pair of public and private keys, public key need to paste into the gitbub, and establish a connection github

3, the configuration keys (GitHub)

The public key generated in Step 2, using txt document opens, copy and paste to github, as shown in operation, the other can not find the setting, please click on the picture identification

Click Add SSH Key, as shown below, successfully added the public key, on github can add more public

4, test connection (Git)

In order to verify whether the local successfully connected to github, enter the following command to verify

$ ssh -T [email protected]

If the following prompt appears, indicating that the local connection to github success, then you can try to upload the code

The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Hi swadian2018! You've successfully authenticated, but GitHub does not provide shell access.

5, upload the code (git)

(1) initialize a local git repository

In the uploaded project folder, open the git command window, type the following command to initialize git, git initialize the local repository

$ git init

There are suggested that the local warehouse successful initialization

Initialized empty Git repository in D:/IdeaWorkSpase/springbootdemo/.git/

初始化后,文件夹下会多了一个.git的隐藏文件夹,如下图所示:

 

(2)尝试连接和代码上传

创建连接用户

尝试连接成功后,我们需要在git上创建连接的用户,--global代表全局使用。

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

此处的“your name”使用的是刚注册的github的name,E-mail也是注册github的e-mail(当然你也可以使用别的name和e-mail,这个只是使用git时需要创建一个用户信息,另,需要修改用户信息时,直接使用原命令覆盖就可以了^_^)

输入连接命令

输入完成后,回到github创建仓库后的页面,在...or push an existing repository from the command line 一栏可以看到如下命令:

$ git remote add origin https://github.com/swadian2018/springbootdemo.git
$ git push -u origin master

把上边两条命令复制到git命令窗口,会弹出输入github的用户名和密码的窗口,如图

输入完后才能后,命令窗口会出现以下提示,文件上传成功!

Enumerating objects: 23, done.
Counting objects: 100% (23/23), done.
Delta compression using up to 4 threads
Compressing objects: 100% (16/16), done.
Writing objects: 100% (23/23), 3.85 KiB | 328.00 KiB/s, done.
Total 23 (delta 0), reused 0 (delta 0)
To https://github.com/swadian2018/springbootdemo.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

下面刷新下github的页面,可以看到上传的文件

二、常见错误提示

1、没有匹配到任何资源

error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/swadian2018/springbootdemo.git'

git初始化提示信息为empty时,实际上这个文件夹下的代码是没有提交到本地仓库的,这个时候要把本地代码git commit 到本地仓库,如果使用的是idea,可以看到未提交的代码都成红色提示。

但有时候使用git init命令初始化本地仓库时,idea的代码却没有任何反应,这个时候可以删除代码文件夹下的.git文件夹,重新启动idea,使用idea工具进行git仓库的初始化。

2、git找不到本地仓库

$ git remote add origin https://github.com/swadian2018/springbootdemo.git
fatal: not a git repository (or any of the parent directories): .git

这个原因是git打开命令窗口时没有在本地仓库所在的位置打开,因此找不到本地仓库的资源。

解决方案:重新初始化git本地仓库,或者在有初始化本地仓库的资源下重新打开命令窗口。

3、提交没授权

这种情况一般是在连接github远程仓库时,输入的连接地址形式不支持时出现的

github初始连接时,支持以下两种方式进行连接,但有时候对于第一种连接方式并不支持,那就需要修改成第二种方式

第一种连接方式
$ git remote add origin [email protected]:swadian2018/springbootdemo.git
第二种连接方式
$ git remote add origin https://github.com/swadian2018/springbootdemo.git

使用如下命令修改连接地址:

查看当前的连接地址

$ git remote -v 

修改当前的连接地址

$ git remote set-url origin https://github.com/youname/warehousename.git

$ git push -u origin master

或者

$ git remote set-url origin [email protected]:youname/warehousename.git

$ git push -u origin master

 

Guess you like

Origin blog.csdn.net/swadian2008/article/details/94996390