本地 Git 与 GitHub 服务器之间保持通信时,使用 SSH key 认证方式来保证通信安全, 所以在使用 GitHub 前读者必须先创建自已的 SSH key。
我们后续的演示会在 Windows 环境下进行,打开 Git Bash,如图 13.3 所示。
图 13.3 Git Bash 界面
fnngj@FNNGJ-PC ~
$ cd ~/.ssh
fnngj@FNNGJ-PC ~/.ssh
$ pwd
/c/Users/fnngj/.ssh
如果你已经有了一个秘钥(默认秘钥文件位置在 C:/Users/fnngj/.ssh/id_rsa)
fnngj@FNNGJ-PC ~/.ssh
$ ssh-keygen -t rsa -C "fnngj@126.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/fnngj/.ssh/id_rsa): --回车
Enter passphrase (empty for no passphrase): --回车
Enter same passphrase again: --回车
Your identification has been saved in /c/Users/fnngj/.ssh/id_rsa.
Your public key has been saved in /c/Users/fnngj/.ssh/id_rsa.pub.
The key fingerprint is:
78:51:9b:2c:6c:fb:74:0b:6b:b9:c4:23:8f:5e:10:6b fnngj@126.com
The key‘s randomart image is:
+--[ RSA 2048]----+
| . |
| . o o |
| * + |
| o * |
| . E o . |
| o = = . |
| . X . |
| B o |
| .o o |
+-----------------+
fnngj@FNNGJ-PC ~/.ssh
$ ls
id_rsa id_rsa.pub
查看目录下会生成两个文件, id_rsa 是私钥, id_rsa.pub 是公钥。记住千万不要把私钥 文件 id_rsa 透露给任何人。
用文本工具打开公钥文件~/.ssh/id_rsa.pub,复制里面的所有内容到剪贴板,如图 13.4 所示。 登录 GitHub, 单击右上角个人头像→Settings→SSH Keys→Add SSH Keys , 在 Title 文 本框中输入任意字符,在 Key 文本框粘贴刚才复制的公钥字符串,单击“Add key”按钮 完成操作,如图 13.5 所示。
fnngj@FNNGJ-PC ~/.ssh
$ ssh -T git@github.com
The authenticity of host ‘github.com (192.30.252.129)‘ can‘t be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,192.30.252.129‘ (RSA) to the list of know
n hosts.
Hi defnngj! You‘ve successfully authenticated, but GitHub does not provide shell
access.
fnngj@FNNGJ-PC ~/.ssh
$ git config --global user.name "fnngj"
fnngj@FNNGJ-PC ~/.ssh
$ git config --global user.email "fnngj@126.com"
在本地创建一个“project-name”项目,与 GitHub 上创建的项目名保持一致。在项目下创建一个 test_case.py 文件。
fnngj@FNNGJ-PC /d/project-name
$ ls
test_case.py
fnngj@FNNGJ-PC /d/project-name
$ git init
Initialized empty Git repository in d:/project-name/.git/
fnngj@FNNGJ-PC /d/project-name (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
test_case.py
nothing added to commit but untracked files present (use "git add" to track)
fnngj@FNNGJ-PC /d/project-name (master)
$ git add .
fnngj@FNNGJ-PC /d/project-name (master)
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test_case.py
fnngj@FNNGJ-PC /d/project-name (master)
$ git commit -m "first commit file"
[master (root-commit) 9b4b839] first commit file
1 file changed, 9 insertions(+)
create mode 100644 test_case.py
如果是第一次提交项目,通过这一行命令将本地的项目与远程的仓库建立连接。此处使用 SSH 方式进行连接:
git remote add origin git@github.com:defnngj/project-name.git
将本地的项目提交到远程仓库的主分支。
git push -u origin master
原文:https://www.cnblogs.com/TomBombadil/p/11006455.html