1. 创建SSH Key
在用户主目录下(打开命令行窗口的默认路径),看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa
和id_rsa.pub
这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:ssh-keygen -t rsa -C "youremail@example.com",一路回车键,效果如图。

然后就可以用记事本打开id_rsa.pub文件,复制文本内容。
2.在GitHub中加入SSH Key
登录GitHub,点击个人头像,选择Settings,在“SSH And GPG keys”页面,点击“New SSH Key”按钮,在Key文本框粘贴id_rsa.pub
文件的内容:
完成后如图

3. 创建一个新的仓库
在GitHub创建一个learngit的仓库


4. 设置远程库
在本地的git仓库运行命令
git remote add origin git@github.com:mqxu/learngit.git
远程库的名字是origin
,这是Git默认的叫法,也可以改成别的,但是origin
这个名字一看就知道是远程库。后面是使用Git协议,注意账号和仓库名的修改
5.把本地库的所有内容推送到远程库上
git push -u origin master

然后去GitHub远程库刷新一下,可以看到文件已经同步了!

注意:
- 一定要本地仓库先提交,然后才能向远程仓库push
- 如果push失败,应该是远程库版本比本地库新,应该用git pull先把远程库拉下来,然后再push
git pull origin master
-
可以从远程库克隆一个到本地库
image

6. 提交helloworld项目到远程仓库
- Git Bash切换到helloworld项目目录
- git config core.autocrlf true
- git add .
- git commit -m "commit helloworld project"
- 远程建立helloworld仓库
- git remote add origin git@github.com:mqxu/helloworld.git
- git push -u origin master
网友评论