Git仓库-远程仓库

远程仓库是指托管在因特网或其他网络中的你的项目的版本库。本节只介绍一些很基础的操作应用。

说明:这里我们用github任务远程仓库,需要注册github账号。

添加远程库

为了便于管理,Git要求每个远程主机都必须指定一个主机名。git remote命令就用于管理主机名。

  1. 在github创建一个新库
    请参见:Create A Repo
  2. 关联本地仓库到GitHub仓库
    运行 git remote add [remote-name] [url] 添加一个新的远程Git仓库,有以下两种方式:
  • HTTPS

    1
    $ git remote add origin https://github.com/yongfeiuall/yongfeiuall.github.io.git
  • SSH
    本地Git仓库和GitHub仓库之间的传输是通过SSH加密,需要设置:Generating SSH keys

    1
    $ git remote add origin git@github.com:yongfeiuall/yongfeiuall.github.io.git

添加后,远程库的名字就是origin,这是Git默认的叫法,也可以改成别的。

Notes: 使用https除了速度慢以外,还有个最大的麻烦是每次推送都必须输入口令,但是在某些只开放http端口的公司内部就无法使用ssh协议而只能用https。

推送到远程库

用 git push [remote-name] [local branch-name] 推送本地仓库到GitHub仓库。

1
$ git push -u origin master

由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

从远程仓库中抓取与拉取

1
$ git fetch origin

必须注意 git fetch [remote-name] 命令会将数据拉取到你的本地仓库 - 它并不会自动合并或修改你当前的工作。 可以使用 git pull [remote-name] [local branch-name] 命令来自动的抓取然后合并远程分支到当前分支。

1
$ git pull origin

克隆远程库

如果你想获得一份已经存在了的Git仓库的拷贝,就要用到git clone命令。

1
$ git clone https://github.com/yongfeiuall/yongfeiuall.github.io.git

远程仓库的移除与重命名

运行 git remote rename [old-remote-name] [new-remote-name] 重命名一个远程仓库

1
$ git remote rename origin newName

运行 git remote rm [remote-name] 删除一个远程仓库

1
$ git remote rm origin

在网上看到一篇很不错的文章,可参考原文学习:Git远程操作详解

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!