1. 配置用户名和邮箱

本地需要配置git的user.name和user.email两个变量,表明自己的身份,对于远程仓库进行更改之后,作为操作者的信息,这两个和github的用户名和email没有任何关系,可以是完全自创的,仅仅作为身份的标识

查看当前的name 和 email

1
2
git config user.name
git config user.email

或者

1
git config --list

新安装的会显示空,如下

1
2
[liode@fedora:4:~]$ git config user.name
[liode@fedora:5:~]$ git config user.email

设置name和email

1
2
git config --global user.name liodegwin
git config --global user.email liodegwin@gmail.com

会显示正确的字符串

1
2
3
4
[liode@fedora:13:~]$ git config user.name
liodegwin
[liode@fedora:14:~]$ git config user.email
liodegwin@gmail.com

同时会在用户的家目录创建好配置文件.gitconfig,里面记录的就是该用户的信息

1
2
3
4
[liode@fedora:15:~]$ cat .gitconfig
[user]
name = liodegwin
email = liodegwin@gmail.com
  1. 创建远程仓库

在github中创建远程仓库,获取到ssh地址,这是一个私有的仓库,对于没有获得权限的用户,不能克隆

1
git@github.com:liodegwin/blogs.git
  1. 配置连接参数

对于私有的项目,需要参与的用户具有访问的权限,这很好理解。因此,我们首先需要创建一个ssh密钥,然后远程仓库将这个密钥授予权限。

创建本地的密钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[liode@fedora:30:~/Documents/blogs]$ ssh-keygen -t rsa -C "liodegwin@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/liode/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/liode/.ssh/id_rsa
Your public key has been saved in /home/liode/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:wACNFWp+VdJtQS5kgVMQnjfQ2gD62sPGKuMsAeERygc liodegwin@gmail.com
The key's randomart image is:
+---[RSA 3072]----+
| Eo==o=B*+o. |
|+.oo.++Bo.o |
|oo=. *=+.. |
|.+.. ..o.o |
|. . o S |
|. * |
| .. * |
|= o . |
|o=. |
+----[SHA256]-----+

在~/.ssh下产生了两个文件,公钥id_rsa.pub和私钥id_rsa,复制其中公钥的内容,在github中将这个ssh key和github账户绑定,这样就能clone私密的仓库了

==注意:ssh公钥是和github账号绑定的,而不是和repository绑定,也就是说,添加了这个公钥之后,所有的私有项目都具有权限!==

  1. 本地修改,提交到远程

首先,克隆在远端的仓库:git clone ***

1
2
3
4
5
6
[liode@fedora:70:~/Documents/blogs]$ tree
.
└── blogs
└── README.md

1 directory, 1 file

里面只有一个README.md,现在,添加两个新的文件,然后将更新至远程仓库

手动复制了两个文件到blogs文件夹下面,然后查看git的状态

1
2
3
4
5
6
7
8
9
10
11
12
simon@DESKTOP-SLHL7P8 MINGW64 /d/MyDocument/blogs/blogs (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)

Qtnote1.md
SSH.md

nothing added to commit but untracked files present (use "git add" to track)

看到有两个文件是untracked,使用git add –all 或者 git add -A跟踪这两个文件,这个命令执行之后没有任何反馈信息输出,查看现在的状态

1
2
3
4
5
6
7
8
9
simon@DESKTOP-SLHL7P8 MINGW64 /d/MyDocument/blogs/blogs (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: Qtnote1.md
new file: SSH.md

现在将更改commit到本地仓库,并查看状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
simon@DESKTOP-SLHL7P8 MINGW64 /d/MyDocument/blogs/blogs (main)
$ git commit -m "qt and ssh record"
[main dcb49eb] qt and ssh record
2 files changed, 47 insertions(+)
create mode 100644 Qtnote1.md
create mode 100644 SSH.md

simon@DESKTOP-SLHL7P8 MINGW64 /d/MyDocument/blogs/blogs (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
  1. 远程的内容覆盖本地的内容

现在本地的branch is ahead of 远程的了,将更新提交到远程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
simon@DESKTOP-SLHL7P8 MINGW64 /d/MyDocument/blogs/blogs (main)
$ git push
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), 1.17 KiB | 1.17 MiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:liodegwin/blogs.git
18cd641..dcb49eb main -> main

simon@DESKTOP-SLHL7P8 MINGW64 /d/MyDocument/blogs/blogs (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
  1. 总结和展望