Git教程-Git&Github尝鲜

****暂时没有时间排版抱歉,关注微信公众号号阅读吧:
扫描下方微信公众号二维码获取更多信息:
Git教程-Git&Github尝鲜

​一、Github和Git之间的桥梁

注册一个Github账号并切换到SSH设置页面,点击New SSH Key:

在本地Git创建SSH Key用于本地和远程仓库之间的传输(注意邮件地址是注册Git时用的邮件地址;如果你有众多设备,按此处操作步骤添加多个SSH Key到Github即可):
[email protected] MINGW64 ~/Desktop
2$ ssh-****** -t rsa -C “[email protected]
3Generating public/private rsa key pair.
4Enter file in which to save the key (/c/Users/Steven/.ssh/id_rsa):
5Created directory ‘/c/Users/Steven/.ssh’.
6Enter passphrase (empty for no passphrase):
7Enter same passphrase again:
8Your identification has been saved in /c/Users/Steven/.ssh/id_rsa
9Your public key has been saved in /c/Users/Steven/.ssh/id_rsa.pub
10The key fingerprint is:
11SHA256:5xZC4/syZWfL1OgYUEli6hiBnpaoaPMmfSxem9lOY44 [email protected]
12The key’s randomart image is:
13±–[RSA 3072]----+
14| … o… |
15| . . o .o |
16| o o. . o. |
17|. = + o… |
18|o. . . S.o o |
19|oo =+.= . |
20|. + … +.ooO . |
21| . =.oX +o. + |
22| +.oE.+ o. |
23±—[SHA256]-----+
不出意外的话会在用户主目录下面生成一个.ssh文件夹,文件夹中包含id_rsa和id_rsa.pub两个文件:

使用记事本或者Notepad++等打开id_rsa.pub文件,全选复制里面内容并添加到Github的SSH设置中:

添加好后显示如下页面:

检查是否绑定成功,第一次绑定需要输入yes继续:
[email protected] MINGW64 ~/Desktop
2$ ssh -T [email protected]
3The authenticity of host ‘github.com (13.250.176.223)’ can’t be established.
4RSA key fingerprint is SHA256:nThcg6kXUpJWGl7E2IGOCspRomTxdCARLviKw6E5SY8.
5Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
6Warning: Permanently added ‘github.com,13.250.177.223’ (RSA) to the list of known hosts.
7Hi madmanazo! You’ve successfully authenticated, but GitHub does not provide shell access.

二、Git初始化设置

一句安装完Git之后,要做的第一件事就是设置你的用户名和邮件地址(要和注册Github的邮件地址对应)。这一点很重要,因为每一次Git提交都会使用这些信息,它们会写入到你的每一次提交中,不可更改:
[email protected] MINGW64 ~/Desktop
2$ git config --global user.name “madmanazo”
3
[email protected] MINGW64 ~/Desktop
5$ git config --global user.email [email protected]
–global选项指全局使用,当你想针对特定项目使用不同的用户名称与邮件地址时,可以在那个项目目录下运行没有 --global 选项的命令来配置
检查Git配置信息:
[email protected] MINGW64 ~/Desktop
2$ git config --list
3diff.astextplain.textconv=astextplain
4filter.lfs.clean=git-lfs clean – %f
5filter.lfs.smudge=git-lfs smudge – %f
6filter.lfs.process=git-lfs filter-process
7filter.lfs.required=true
8http.sslbackend=openssl
9http.sslcainfo=D:/software/Git/Git/mingw64/ssl/certs/ca-bundle.crt
10core.autocrlf=true
11core.fscache=true
12core.symlinks=false
13credential.helper=manager
14user.name=madmanazo
[email protected]

三、获取Git仓库

可以在Github创建一个空仓库(公有的或者私有的)并进行管理:

切换到你想要保存的一个本地仓库目录下,如果没有就新建一个:
[email protected] MINGW64 ~/Desktop
2$ cd /D/data/work
[email protected] MINGW64 /D/data/work
[email protected] MINGW64 /D/data/work
5$ mkdir ElectricDeveloper
6
[email protected] MINGW64 /D/data/work
8$ ls
9 ElectricDeveloper/ ‘embedded software’/ hardware/
10
[email protected] MINGW64 /D/data/work
12$ cd ElectricDeveloper/
根据Github上关于ElectricDeveloper仓库初始化的提示创建一个README.md文件用于测试:

[email protected] MINGW64 /D/data/work/ElectricDeveloper
2$ echo “# ElectricDeveloper” >> README.md
初始化本地测试仓库:

[email protected] MINGW64 /D/data/work/ElectricDeveloper
2$ git init
3Initialized empty Git repository in D:/data/work/ElectricDeveloper/.git/
将README.md文件添加到下一次提交中以进行管理:

[email protected] MINGW64 /D/data/work/ElectricDeveloper (master)
2$ git add README.md
添加一个本次上传备注信息:

[email protected] MINGW64 /D/data/work/ElectricDeveloper (master)
2$ git commit -m “first commit”
3[master (root-commit) 050865a] first commit
4 1 file changed, 1 insertion(+)
5 create mode 100644 README.md
本地仓库与远程仓库关联:

[email protected] MINGW64 /D/data/work/ElectricDeveloper (master)
2$ git remote add origin https://github.com/madmanazo/ElectricDeveloper.git
将本地仓库的所有内容推送到远程仓库(第一次推送到远程需要加上-u参数以关联本地和远程的master,之后推送不需要再加-u参数):

[email protected] MINGW64 /D/data/work/ElectricDeveloper (master)
2$ git push -u origin master
3Enumerating objects: 3, done.
4Counting objects: 100% (3/3), done.
5Writing objects: 100% (3/3), 227 bytes | 227.00 KiB/s, done.
6Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
7To https://github.com/madmanazo/ElectricDeveloper.git
8 * [new branch] master -> master
9Branch ‘master’ set up to track remote branch ‘master’ from ‘origin’.
打开远程仓库查看效果:

克隆远程仓库到本地某个目录下并进行管理:

[email protected] MINGW64 /D/data/work/hardware
2$ git clone https://github.com/madmanazo/ElectricDeveloper.git
3Cloning into ‘ElectricDeveloper’…
4remote: Enumerating objects: 3, done.
5remote: Counting objects: 100% (3/3), done.
6remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
7Receiving objects: 100% (3/3), done.
地址(使用SSH速度会比HTTPS速度快一些)要对应上才能成功,打开本地对应目录可以看到文件已存在:

热爱科技,享受生活

——madmanazo