Git远程仓库(github)的使用

在本地安装了Git后,可以在本地建立仓库管理代码。但是,通常情况下,我们的代码都是在服务器上的(比如github),那么我们就要建立本地仓库和github服务器的连接,这样就可以把我们的代码推到github上。

 

1. 创建ssh key

因为本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以首先在本地创建一个ssh key,然后放在github上去。

$ ssh-keygen -t rsa -C "github_email@email.com"    //Github上注册的邮箱

一直默认回车就行,成功后会生成在 ~/.ssh/id_rsa.pub

然后,进入github设置keys,把上面的这一串复制进去就好  https://github.com/settings/keys 

设置好就OK了。

验证是否成功,在git bash下输入:
$ ssh -T git@github.com
... You've successfully authenticated,...
这就表示已经成功连上github了。


2. 添加远程仓库
$ git remote add [shortname] [url]
url格式是 git@github.com:yourName/yourRepo.git
yourName和yourRepo表示你再github的用户名和刚才新建的仓库,加完之后进入.git如:$ git remote add origin git@github.com:millerying/HelloMe

$ git remote //查看当前有哪些仓库命令
$ git remote -v  //-v 参数,你还可以看到每个别名的实际链接地址。
origin  https://github.com/millerying/HelloMe.git (fetch)
origin  https://github.com/millerying/HelloMe.git (push)
  
打开config,这里会多出一个remote "origin"内容,这就是刚才添加的远程地址,也可以直接修改config来配置远程地址。

[remote "origin"]
url = https://github.com/keyying/HelloMe.git
fetch = +refs/heads/*:refs/remotes/origin/*


3. 提取远程仓库
$ git fetch  //从远程仓库下载新分支与数据
该命令执行完后需要执行git merge 远程分支到你所在的分支。

$ git pull  //从远端仓库提取数据并尝试合并到当前分支
该命令就是git fetch + git merge


4. 提交到远程仓库
$ git push [alias] [branch]
将你的 [branch] 分支推送成为 [alias] 远程仓库上的 [branch] 分支
如:git push origin master


5.删除远程仓库
$ git remote rm [alias]

 

posted @ 2017-09-21 23:55  一路向北v  阅读(241)  评论(0编辑  收藏  举报