个人博客搭建之二:Git绑定GitHub基本操作
利用SSH来完成GitHub的绑定并提交文件:
参考链接
- 枫叶知乎博客学习专栏:八篇文章构成的知乎专栏,作者
枫叶
,手把手指导从github建立个人博客,以及相关美化操作 - Git官网:Git is a free and open
source distributed version control system ...
[一个免费开源的分布式版本控制系统]
- Git维基:Git的wikipedia页面
- My GitHub:我的GitHub主页
开始条件
本人具备的资源和知识条件(2022-5-7)
- Github已有注册账户,可方便登录访问;
- 原购买一个域名nyjing.online,闲置中;
- MarkDown基本了解;
- HTML、CSS知道些皮毛;
- JavaScript等基本不懂。
- Git工具了解一些:《个人博客搭建之一:git工具》
Github绑定
对应枫叶知乎博客学习专栏的第3篇;
日期:2022-5-7
利用SSH来完成GitHub的绑定并提交文件。
SSH(安全外壳协议,Secure Shell) 是建立在应用层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议,利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。简单来说,SSH就是保障你的账户安全,将你的数据加密压缩,不仅防止其他人截获你的数据,还能加快传输速度。
本地Git生成密钥
用git上传文件到GitHub首先得利用SSH登录远程主机,而登录方式有两种:一种是口令登录;另一种是公钥登录。口令登录每次都要输入密码十分麻烦,而公钥登录就省去了输入密码的步骤,所以我们选择公钥授权。首先我们得在 GitHub 上添加 SSH key 配置,要想生成SSH key,就要先安装 SSH,不过我们安装了 Git Bash,其应该自带了 SSH。
检验一下是否安装 SSH,我们在新建的文件夹中右键打开 Git Bash,输入
ssh
命令,查看本机是否安装 SSH
1 | JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text |
即已安装 SSH. 然后,输入 ssh-keygen -t rsa
命令(注意空格),表示我们指定 RSA
算法生成密钥,然后敲四次回车键,之后就就会生成两个文件,分别为秘钥
id_rsa
和公钥 id_rsa.pub
.
(注意:git中的复制粘贴不是 Ctrl+C
和
Ctrl+V
,而是 Ctrl+insert
和
Shift+insert
.)文件的位置在 Git Bash
上面都有显示,默认生成在以下目录:
- Linux 系统:~/.ssh
- Mac 系统:~/.ssh
- Windows 10 :C:/Users/ASUS/.ssh
1 | ssh-keygen -t rsa |
可以通过目录找到该文件的位置,用记事本打开文件。也可以按照以下步骤直接在 Git Bash 上打开就行:
1 | cd ~/.ssh |
Github操作
- 进入个人 GitHub 主页,点击右上角头像,再点击 settings
,进入
settings/profile
页面: - 点击 SSH and GPG keys,再点击 New SSH key;
- 复制的公钥
id_rsa.pub
的内容粘贴到 key 内,添加 Title 内容,再点击 Add SSH key,
验证是否成功,可以通过在 Git Bash 中输入
ssh -T git@github.com
进行检验:
1 | JING@LAPTOP-DEHH91FV MINGW64 ~/.ssh |
第一次输入提示Are you sure you want to continue connecting (yes/no/[fingerprint])?
,填
yes
就行,若出现Permanently added 'github.com' (ED25519) to the list of known hosts
,则表明绑定成功。
提交文件方法测试
Case 1:本地没有 git 仓库
方法:
- 直接将远程仓库 clone 到本地;
- 将文件添加并 commit 到本地仓库;
- 将本地仓库的内容push到远程仓库。
操作:
进入GitHub个人主页,进入新建的 text 项目;
点击 Code,复制 Clone --> https 地址;
进入我们准备存储 Git 仓库的本地目录text,右键启动 Git Bash,输入
git clone https://github.com/nyjingle/text.git
,将远程仓库 clone 到本地1
2
3
4
5
6
7JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text
git clone https://github.com/nyjingle/text.git
Cloning into 'text'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.clone 到本地的仓库内容与远程仓库的内容完全一致。
本地目录
git_codelife/text/text
下创建一个text.txt
文件,并启动Git Bash,输入git status
命令查看仓库状态1
2
3
4
5
6
7
8
9
10
11
12
13JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text
cd text
JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text/text (main)
git status
On branch main
Your branch is up to date with 'origin/main'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
text.txt
nothing added to commit but untracked files present (use "git add" to track)text 已经是一个 Git 仓库了,而刚刚创建的文件
text.txt
没有被追踪,也就是没有提交到本地仓库。使用
git add
命令将文件添加到了「临时缓冲区」,再用git commit -m
"提交信息" 将其提交到本地仓库1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text/text (main)
git add text.txt
JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text/text (main)
git commit -m "commit text file"
Author identity unknown
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'JING@LAPTOP-DEHH91FV.(none)')第一次提交,作者身份未知,按提示配置后,重新运行
git commit -m
1
2
3
4
5
6
7
8
9
10
11JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text/text (main)
git config --global user.email "nyjingle@gmail.com"
JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text/text (main)
git config --global user.name "Jun Jing"
JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text/text (main)
git commit -m "commit text file"
[main 41f6e12] commit text file
1 file changed, 3 insertions(+)
create mode 100644 text.txt输入
git log
命令查看仓库提交日志:1
2
3
4
5
6
7
8
9
10
11
12
13JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text/text (main)
git log
commit 41f6e12ff8d728c6155b5ed31b032abab0d9441a (HEAD -> main)
Author: Jun Jing <nyjingle@gmail.com>
Date: Sun May 8 22:23:15 2022 +0800
commit text file
commit 5190370d385040820de381d454c017416fde97c6 (origin/main, origin/HEAD)
Author: Jun Jing <nyjingle@gmail.com>
Date: Sun May 8 11:41:03 2022 +0800
Initial commit再输入 git status 查看一下仓库状态:
1
2
3
4
5
6
7JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text/text (main)
git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean本地仓库超前
输入
git push origin main
命令,将本地仓库提交到远程仓库,origin
是远程主机的名字,main
是本地分支名字:1
2
3
4
5
6
7
8
9
10JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/text/text (main)
git push origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 309 bytes | 309.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/nyjingle/text.git
5190370..41f6e12 main -> main第一次
push
需要授权git访问github。
github 上检查text仓库,测试文件text.txt已上传加入。
Case 2:本地有 Git 仓库,并且已经进行了多次 commit 操作
步骤:
- 建立一个本地仓库进入,init 初始化;
- 关联远程仓库;
- 同步远程仓库和本地仓库;
- 将文件添加提交到本地仓库;
- 将本地仓库的内容 push 到远程仓库。
操作:
建立一个本地仓库
demo
,使用git init
命令初始化这个仓库;1
2
3JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo
git init
Initialized empty Git repository in D:/MyStuday/git_codelife/demo/.git/输入
git remote add origin https://github.com/nyjingle/text.git
命令,关联远程仓库,接着输入git pull origin main
命令,同步远程仓库和本地仓库,1
2
3
4
5
6JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git remote add origin https://github.com/nyjingle/text.git
JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git pull origin main
fatal: unable to access 'https://github.com/nyjingle/text.git/': OpenSSL SSL_read: Connection was reset, errno 10054出接入错误。关闭Git Bash,在本地仓库目录中重新启动,再次输入 pull 指令,成功
1
2
3
4
5
6
7
8
9
10JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git pull origin main
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 858 bytes | 26.00 KiB/s, done.
From https://github.com/nyjingle/text
* branch main -> FETCH_HEAD
* [new branch] main -> origin/main本地仓库
demo
中,发现已经把远程仓库(text
)的内容同步到了此处。
另注意,本地仓分支名称(master)也与远程仓库分支名称(main)不一致,这不影响同步操作。接下来的步骤就与 Case 1 方法一样(可以参考上面),先输入 git add 和 git commit 命令,将要提交的文件添加并提交到 demo 仓库;然后再输入 git push origin main 命令,将本地仓库修改(或者添加)的内容提交到远程仓库!!!
具体设置:在demo下新建picture文件夹,并拷贝入一png图片。—— 与Case 1 单一文件不同。
添加文件夹指令:
git add picture/
1
2
3
4
5
6
7
8
9
10JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git add picture/
JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: picture/authorize_git_credential_manager.png
new file: picture/read_me.txt提交:
git commit -m
指令后双引号内容为加入的说明文字;
1
2
3
4
5
6JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git commit -m "commit picture fold"
[master ac688a8] commit picture fold
2 files changed, 3 insertions(+)
create mode 100644 picture/authorize_git_credential_manager.png
create mode 100644 picture/read_me.txtgit log
查看日志1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git log
commit ac688a8f7b1dd70657c24b89790fa5c637d48662 (HEAD -> master)
Author: Jun Jing <nyjingle@gmail.com>
Date: Mon May 9 01:27:10 2022 +0800
commit picture fold
commit 41f6e12ff8d728c6155b5ed31b032abab0d9441a (origin/main)
Author: Jun Jing <nyjingle@gmail.com>
Date: Sun May 8 22:23:15 2022 +0800
commit text file
commit 5190370d385040820de381d454c017416fde97c6
Author: Jun Jing <nyjingle@gmail.com>
Date: Sun May 8 11:41:03 2022 +0800
Initial commitgit push origin main
将本地仓库同步到远程仓库1
2
3
4
5
6
7
8
9
10
11
12
13
14JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git push origin main
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/nyjingle/text.git'
JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git status
On branch master
nothing to commit, working tree clean
JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git push origin master
fatal: unable to access 'https://github.com/nyjingle/text.git/': OpenSSL SSL_read: Connection was reset, errno 10054出错。检查状态, 并未提示本地仓ahead。—— 因远程仓并无与本地仓同名的master分支?
修正 push 仓库名称为 master (push的是本地仓,所以应使用本地仓分支名称),提示不可达错误 —— 关闭并重新启动Git bash后执行ok!1
2
3
4
5
6
7
8
9
10
11
12
13
14JING@LAPTOP-DEHH91FV MINGW64 /d/MyStuday/git_codelife/demo (master)
git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 120.86 KiB | 17.27 MiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: https://github.com/nyjingle/text/pull/new/master
remote:
To https://github.com/nyjingle/text.git
* [new branch] master -> master即本地demo的master分支上传到远程仓库text,在github上,text项目下多了个新的master分支,与本地仓一致。