一、配置ssh key
1. 进入当前用户目录
cd ~
2. 生成ssh key
ssh-keygen -t rsa -C "ABC@qq.com"
ABC@qq.com账号必须是你登录github的账户名
中间如果提示文件已经存在可以选择覆盖,提示输入密码,可以直接按Enter跳过。
$ ssh-keygen -t rsa -C "ABC@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/yuanlong.zhang/.ssh/id_rsa):
/c/Users/yuanlong.zhang/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/yuanlong.zhang/.ssh/id_rsa.
Your public key has been saved in /c/Users/yuanlong.zhang/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:TQlJPqZ6GF/zl3aJHpZmzfc39NfRs8rzfEfGCJBVmR0
ABC@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| .o. o...E.|
| ...o. o .|
| + o. |
| o + . |
| . . S . . o.|
| = . o *.==|
| o o . @ *oB|
| . B.+.+O|
| .o+oB|
+----[SHA256]-----+
id_rsa
id_rsa.pub
known_hosts
打开公钥文件id_rsa.pub,并拷贝里面的内容。
4. 使用上面的ABC@qq.com账户登录github网站,在Settings->SSH and GPH keys里面添加SSH key,内容就是上一步中的id_rsa.pub的文件内容。
5. 测试ssh key是否设置成功
输入ssh -T git@github.com,输出:
$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.
Hi ABC! You've successfully authenticated, but GitHub does not provide shell access.
如果有hi,ABC字样,则表示连接成功。如果有如上文的警告,则可以打开/etc/hosts文件,将上面的ip地址192.30.255.112与github.com绑定,即在/etc/hosts文件中增加一行:
192.30.255.112 github.com
再次输入ssh -T git@github.com,就不会有这个警告了:
$ ssh -T git@github.com
Hi ABC! You've successfully authenticated, but GitHub does not provide shell access.
二、从远程github仓库拉取文件
1. 在本地新建一个目录, 例如叫learngit
2. 进入该目录打开git bash,输入git init,建立一个空的仓库。
3. 然后克隆远程文件,例如repos的地址是git@github.com:baloonwj/flamingo.git,输入命令:
git clone git@github.com:baloonwj/flamingo.git
注意 git@github.com是规定写法,不是 你的用户名@github.com。这样我们就可以拉取到文件了。
$ git clone git@github.com:baloonwj/flamingo.git
Cloning into 'flamingo'...
remote: Counting objects: 15, done.
remote: Total 15 (delta 0), reused 0 (delta 0), pack-reused 15
Receiving objects: 100% (15/15), 13.37 MiB | 564.00 KiB/s, done.
Resolving deltas: 100% (2/2), done.
三、本地向远程github仓库提交文件
1. 向本地stage增加文件,点号可以换成具体文件的名称(支持文件夹、通配符等)
git add .
如果想撤销,使用git reset .(点号可以换成具体文件的名称(支持文件夹、通配符等))
或者使用git rm --cached <added_file_to_undo>
2. 向本地repos提交
git commit -m "提交日志"
3. 向远程github提交
git push -u origin master
如果本地某些文件不是最新的,可能需要先执行git pull更新一下(可能有冲突,需要自己手动合并一下,并填写合并日志)