6:远程仓库
远程仓库
先去github上注册一个账号,就可以免费创建属于自己的远程仓库
1:远程仓库的建立
1:注册并登录github
2:点击右上角加号,点击new repository,打开新建仓库的页面
3:填写仓库名,例demo,只要自己这个账号下没有重名的项目名就可以使用
4:填写仓库的描述信息
5:public:免费的,开源的,别人可以看见的
6:private:付费的,私有的,别人看不见的
7:Initialize this repository with a README,默认创建README文件
上面5,6二选一
第七步没有选,意味着这个仓库是空的,当与本地仓库关联后,本地代码第一次上传,不需要pull,如果选择,说明该仓库有内容,当与本地仓库关联后,本地代码第一次上传,需要先pull,在push
本地仓库和远程仓库之间的数据传输有两种方式
1:https协议
通过github账号和密码就可以上传下载代码,推荐使用
$ git remote add origin https://github.com/qq1157918444/demo.git
2:ssh协议
1:本地仓库与远程仓库建立关联
$ git remote add origin git@github.com:qq1157918444/demo.git
2:拉取并合并代码
$ git pull
The authenticity of host 'github.com (192.30.252.120)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.120' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
由于本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以,需要一点设置:
3:创建SSH Key
在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:
step1:打开git bash
step2:在跟目录查看.ssh
ls -ah
step3:进入.ssh文件
step4:查看文件
$ ls
known_hosts
step5:没有id_rsa和id_rsa.pub
$ ssh-keygen -t rsa -C "1157918444@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:M3GASaAjIObM31c3sj8Bs6SlJDEUc7L2rsIwsIpvY1I 1157918444@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|o. oXo+. |
|B . X . |
|.+o + ..B.o |
|....o + *oB . |
|.. . . =So . |
|.oE o o. . |
|o.+ . o |
|+ +o . . |
| =.... |
+----[SHA256]-----+
上面又三个需要输入的地方,直接拿回车键即可
step6:查看生成的文件
$ ls
id_rsa id_rsa.pub known_hosts
step7:登录自己的github账号,在setting里面,点击ssh那一项
step8:点击ADD SSH key,填写任意的title名字,在Key文本框里粘贴id_rsa.pub文件的内容,在上面需要输入自己账号的密码,需要确认。
为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的,而Git支持SSH协议,所以,GitHub只要知道了你的公钥,就可以确认只有你自己才能推送。
4:拉取并合并代码
$ git pull
Warning: Permanently added the RSA host key for IP address '192.30.252.122' to the list of known hosts.
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:qq1157918444/demo
* [new branch] master -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
5:由于本地分支和远端分支没有建立track关系
$ git pull origin master
From github.com:qq1157918444/demo
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md
6:推送代码
$ git push origin master
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (17/17), 1.63 KiB | 0 bytes/s, done.
Total 17 (delta 2), reused 0 (delta 0)
To git@github.com:qq1157918444/demo.git
4d6f58c..0ab1909 master -> master
7:登录github,查看是否推送成功
8:远程仓库的克隆
git clone https://github.com/qq1157918444/demo.git