添加远程库
git remote add [shortname] [url]
本地 Git 和 远程仓库间传输通过SSH加密,需配置验证信息:
使用以下命令生成 SSH Key:
$ ssh-keygen -t rsa -C "liuyifeng@17tongx.com"
一路回车就行。 ~/ 下生成 .ssh 文件夹,打开 id_rsa.pub,复制 key。
$ ssh-keygen -t rsa -C "liuyifeng@17tongx.com" Generating public/private rsa key pair. Enter file in which to save the key (/Users/tianqixin/.ssh/id_rsa): Enter passphrase (empty for no passphrase): # 直接回车 Enter same passphrase again: # 直接回车 Your identification has been saved in /Users/tianqixin/.ssh/id_rsa. Your public key has been saved in /Users/tianqixin/.ssh/id_rsa.pub. The key fingerprint is: SHA256:MDKVidPTDXIQoJwoqUmI4LBAsg5XByBlrOEzkxrwARI liuyifeng@17tongx.com The key's randomart image is: +---[RSA 3072]----+ |E*+.+=**oo | |%Oo+oo=o. . | |%**.o.o. | |OO. o o | |+o+ S | |. | | | | | | | +----[SHA256]-----+
github 进入Account => Settings。
左边选择 SSH and GPG keys,然后点击 New SSH key 按钮,title 设置标题,可以随便填,粘贴在你电脑上生成的 key。
添加成功后界面如下所示
为了验证是否成功,输入以下命令:
$ ssh -T git@github.com The authenticity of host 'github.com (52.74.223.119)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes # 输入 yes Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts. Hi tianqixin! You've successfully authenticated, but GitHub does not provide shell access. # 成功信息
以下命令说明我们已成功连上 Github。
之后登录后点击" New repository " 如下图所示:
之后在在Repository name 填入 runoob-git-test(远程仓库名) ,其他保持默认设置,点击"Create repository"按钮,就成功地创建了一个新的Git仓库:
创建成功后,显示如下信息:
以上信息告诉我们可以从这个仓库克隆出新的仓库,也可以把本地仓库的内容推送到GitHub仓库。
推送到远程仓库
推送你的新分支与数据到某个远端仓库命令:
git push [alias] [branch]
以上命令将你的 [branch] 分支推送成为 [alias] 远程仓库上的 [branch] 分支,实例如下。
$ touch runoob-test.txt # 添加文件 $ git add runoob-test.txt $ git commit -m "添加到远程" master 69e702d] 添加到远程 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 runoob-test.txt $ git push origin master # 推送到 Github
重新回到我们的 Github 仓库,可以看到文件已经提交上来了:
删除远程仓库
删除远程仓库你可以使用命令:
git remote rm [别名]
实例
$ git remote -v origin git@github.com:tianqixin/runoob-git-test.git (fetch) origin git@github.com:tianqixin/runoob-git-test.git (push) # 添加仓库 origin2 $ git remote add origin2 git@github.com:tianqixin/runoob-git-test.git $ git remote -v origin git@github.com:tianqixin/runoob-git-test.git (fetch) origin git@github.com:tianqixin/runoob-git-test.git (push) origin2 git@github.com:tianqixin/runoob-git-test.git (fetch) origin2 git@github.com:tianqixin/runoob-git-test.git (push) # 删除仓库 origin2 $ git remote rm origin2 $ git remote -v origin git@github.com:tianqixin/runoob-git-test.git (fetch) origin git@github.com:tianqixin/runoob-git-test.git (push)