Git的使用--上传与下载

配置Git用户名和邮箱

如果是Windows下,选择Git Bash,在命令行中完成一切,可能开始有点麻烦,不过就那几条命令行,用几次就记住啦。首先初始设置Git:

# 配置用户名
git config --global user.name "Your Real Name"
# 配置邮箱 	注:注册时使用的邮箱
git config --global user.email [email protected]

验证:在配置完成后输入

git config --global --list

生成SSH

在窗口的命令行里输入以下的指令并运行,连续点击回车,生成SSH文件位置参照命令提示,默认是系统的用户文件夹

ssh-keygen -C '你注册时使用的邮箱' -t rsa

image-20210722143124823

密钥生成的位置

image-20210722143214003

将ssh文件夹中的公钥( id_rsa.pub)添加到GitHub管理平台中,在GitHub的个人账户的设置中找到如下界面

然后将id_rsa.pub用编辑软件(everedit等编辑软件)打开,将密钥复制到如下的key下面,再点击Add SSH key 生成图二

image-20210722143750413

​ 图一

image-20210722143946675

​ 图二

测试配置是否成功

  1. 在Git Bash中输入:
ssh -T [email protected]
  1. SSH警告

    当你第一次使用Git的命令连接GitHub时,会得到一个警告:

$ ssh -T [email protected]
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?

这是因为Git使用SSH连接,而SSH连接在第一次验证GitHub服务器的Key时,需要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,输入yes回车即可。

Git会输出一个警告,告诉你已经把GitHub的Key添加到本机的一个信任列表里了:

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.

这个警告只会出现一次,后面的操作就不会有任何警告了。

如果你实在担心有人冒充GitHub服务器,输入yes前可以对照GitHub的RSA Key的指纹信息是否与SSH连接给出的一致。

出现以下结果表示配置成功

Hi maoniya! You've successfully authenticated, but GitHub does not provide shell access

上传项目到github仓库

1.选中你需要上传码云仓库的项目

选中此项目点击git bash here,然后在弹窗里进行操作

image-20210728094513838

注意:看个人需求不一样,你的仓库里是放一个项目还是存放多个项目,你在执行git init时生成本地仓库的位置也不一样,有以下俩种情形:

1.1远程仓库只有一个项目的

image-20210728093521821

​ 项目文件夹

image-20210728093658650

1.2远程仓库有多个项目的

image-20210728093949843

image-20210728094021899

此时文件夹作为你的本地仓库,在此文件夹中存放多个项目

2.建立本地仓库

此仓库作为本地存储项目的仓库

git init

初始化一个本地仓库,将需要上传的项目

3.选择要加进仓库的项目

git add maoni_MybatisDemo

如果需要将此目录下的全部上传

git add  all .

. : 全部文件夹

上传仓库时,若出现以下报错

warning: LF will be replaced by CRLF in pom.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in src/main/java/net/maoni/Mybatis/mavenDemo2Application.java.
The file will have its original line endings in your working directory

解决办法:

git config --global core.autocrlf false

4.提交本地仓库,备注说明(不重要)

 git commit -m 'springbootdemo'

-m后面跟一个参数,表示说明,将代码提交到GitHub后,将会在代码文件信息上显示这个说明

image-20210722150406542

5.关联远程仓库

 git remote add origin [email protected]:maoniya/SpringBootDemo01.git

[email protected]:maoniya/SpringBootDemo01.git为你在github上的仓库地址

image-20210722150603865

1.如果出现报错情况如

error: remote origin already exists.

2.则在git bash here窗口里输入

git remote rm origin

3.然后再去重新提交

git remote add origin [email protected]:maoniya/SpringBootDemo01.git

6.执行提交代码前判断

如果gitee仓库创建有README.MD 文件,则先拉取代码,如果仓库是空的则跳过执行下一步

git pull --rebase origin master

7.提交远程仓库

git push -u origin master

image-20210722151111398

下载项目

将项目从gitee或github上下载到本地

1.在本地创建一个目录用来存放项目文件

2.在目录下操作

image-20210722164112416

在窗口中输入:

 git clone '你克隆的地址'

image-20210722164159871

猜你喜欢

转载自blog.csdn.net/m0_49969111/article/details/119002416