安装 Git 工具
配置 Git 帐户
这个操作是一次性的,当然,前提是已经注册过 GitHub 帐号,并且绑定了常用邮箱。这部步,就不赘述了。
- 初始化Git:
git config --global user.name "wubixiaozhu"
git config --global user.email "wubixiaozhu@126.com"
- 创建SSH-Key:
SSH-Key 可以创建多个,并对多个进入管理。我们以「mykey-1」和「mykey-2」这 两个 SSH-Key 为例,说明一下该过程。
从 开始菜单 打开 「Git Bash」 后,你要做的,只有 输入邮箱 和 输入名称 两个动作:
# 输入邮箱
$ ssh-keygen -t rsa -C "wubixiaozhu@126.com"
Generating public/private rsa key pair.
# 输入key名称
Enter file in which to save the key (/c/Users/CatSoft/.ssh/id_rsa): mykey-1
# 一路回车
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in mykey-1.
Your public key has been saved in mykey-1.pub.
The key fingerprint is:
SHA256:HjHveGq+q0ep7SJlTJRdxfXP3WkBWXVGmBCWxhkyvmc ***@qq.com
The key's randomart image is:
+---[RSA 3072]----+
| o .++*O+=*|
| o .. +*.+o.|
| . o .. ..|
| . + . .*|
| o S.o E o=|
| +.o+ o . |
| o +o o |
| . o +o |
| ..B*o |
+----[SHA256]-----+
# 输入邮箱
$ ssh-keygen -t rsa -C "***@qq.com"
Generating public/private rsa key pair.
# 输入名称
Enter file in which to save the key (/c/Users/CatSoft/.ssh/id_rsa): mykey-2
# 一路回车
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in mykey-2.
Your public key has been saved in mykey-2.pub.
The key fingerprint is:
SHA256:/Ji7c2Rm3/P6dWlBj50V4BrF/wzkhB2MeE0w/Wqe4LI ***@qq.com
The key's randomart image is:
+---[RSA 3072]----+
| .+%+o |
| . *oB .|
| o =.o.|
| . o ++=|
| S . .*+|
| += . o =|
| o=.o = +o|
| ..o o * o|
| o+Eo .=o|
+----[SHA256]-----+
CatSoft 是我的 Win10 用户名,在一个类似「C:\Users\CatSoft」这样的目录下,有:
- mykey-1
- mykey-1.pub
- mykey-2
- mykey-1.pub
将它们复制一份,到同目录下的「.ssh」
- 建立 config 管理文件
在「C:\Users\CatSoft\.ssh」下面,建一个没有「后缀」的,名称为「config」的文本文件。
在里面写入:
# key-1
Host key-1
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key-1
# key-1
Host key-01
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key-1
# key-2
Host key-2
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key-2
# key-2
Host key-02
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key-2
# key-3
Host key-3
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key-3
# key-03
Host key-03
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key-3
# key-4
Host key-4
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key-4
# key-04
Host key-04
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key-4
Host 这个,指的是「别名」,就是为了方便管理多个不同的 SSH-Key ,以对应多个不同的 Git 项目而设计的;
HostName 这个,指的是「实际名」,这个必须写成「github.com」,因为这是我们远程时的真实域名。
GitHub 项目,会有使用「SSH」的选项,到时,需要将其中的「github.com」改为你使用的 config 中,写好的「别名」。关于这点,先有个印象,一会儿有详细操作。
- 云端添加「SSH-Key」
你的 GitHub 头像上,点一下,有「Settings」,进入之后,左手边,有「SSH and GPG keys」,从这里进入 SSH-Key 管理页「New SSH Key」,一个绿色的图标,映入眼帘了:
- Title: sshkey-1
- Key: ssh-rsa……qq.com
ssh-rsa……qq.com 是什么呢?
你还记得「mykey-1.pub」这个文件吧,用 notepad++ 打开它,以「ssh-rsa」开头,以「qq.com」结尾。全选,拷贝,粘贴到你的 GitHub 帐户这个「New SSH Key」的「key」值中。
ssh-key-1 就添加成功了,同样的办法,添加 ssh-key-2
这个过程,可能会要求你再输入一次 GitHub 帐户的密码,以确保是帐户本人操作。
建立 GitHub 项目
帐户头像旁边,「new repository」,创建一个新的项目,以「test」为例,如下演示:
- Repository name: test
- Description: test
- Public
- Ctreate repository
建好后,有:
Get started by creating a new file or uploading an existing file. We recommend every repository include a README, LICENSE, and .gitignore.
点击「include a README」,进入「README」文件的编写,随便点一下「Commit new file」。
管理项目
在新建的「test」下,有绿色的按键「Clone or download」,选用「Use SSH」,默认可能是「Use HTTP」,确信看到:
git@github.com:98wb/test.git
这个地址,不能直接用,复制到 notepad++ ,改成:
git@sshkey-1:98wb/test.git
然后再改成:
git clone git@sshkey-1:98wb/test.git
这才是我们需要远程命令,在一个不会轻易删掉的地方,点「右键」,有「Git Bash Here」:
git clone git@sshkey-1:98wb/test.git
应该会看到拉代码的过程了。
这个过程结束后,在本地会生成一个文件夹,你进到里面,这就是刚刚在云端建立的test项目及相关文件了。
你打开「README.md」改一改其中的内容,保存变更。
在「README.md」这个目录下,右键菜单,调出「Git Bash Here」,上传变动:
git status && git pull && git add -A && git commit -a -m"更新" && git push
等结束,去网页里看一看 GitHub 项目里,有没有收到相应的更改。
如果一切正常,把你的文件丢到本地文件夹中,用上传变动的命令,保持云端同步更新。
GitHub 不支持空白文件夹的存在,请勿放入空白的文件夹。
README.md 是 Markdown 语言的文件,可以写图文并茂的说明。
不管如何,先迈出第一步吧,Good Luck !
如果需去除换行符风格检查:
git config --global core.autocrlf false