远程仓库是托管在因特网或网络中的项目的版本库。你可以有多个远程仓库,通常有些对你只读,有些可以读写。

查看远程仓库

如果要查看已经配置的远程仓库服务器,可以运行git remote 命令,它会列出每个远程服务器的缩写。

$ git clone https://github.com/schacon/ticgit
Cloning into 'ticgit'...
remote: Reusing existing pack: 1857, done.
remote: Total 1857 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1857/1857), 374.35 KiB | 268.00 KiB/s, done.
Resolving deltas: 100% (772/772), done.
Checking connectivity... done.
$ cd ticgit
$ git remote
origin

指定选项-v,可以显示远程仓库对应的URL

$ git remote -v
origin    https://github.com/schacon/ticgit (fetch)
origin    https://github.com/schacon/ticgit (push)

如果远程仓库不止一个

$ cd grit
$ git remote -v
bakkdoor  https://github.com/bakkdoor/grit (fetch)
bakkdoor  https://github.com/bakkdoor/grit (push)
cho45     https://github.com/cho45/grit (fetch)
cho45     https://github.com/cho45/grit (push)
defunkt   https://github.com/defunkt/grit (fetch)
defunkt   https://github.com/defunkt/grit (push)
koke      git://github.com/koke/grit.git (fetch)
koke      git://github.com/koke/grit.git (push)
origin    git@github.com:mojombo/grit.git (fetch)
origin    git@github.com:mojombo/grit.git (push)

注意上面的远程仓库使用了不同的协议。

如果想要查看一个远程仓库更多的信息,可以使用git remote show [remote-name] 命令

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/schacon/ticgit
  Push  URL: https://github.com/schacon/ticgit
  HEAD branch: master
  Remote branches:
    master                               tracked
    dev-branch                           tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

它会列出远程仓库的URL与跟踪分支的信息。它有可能会很复杂

$ git remote show origin
* remote origin
  URL: https://github.com/my-org/complex-project
  Fetch URL: https://github.com/my-org/complex-project
  Push  URL: https://github.com/my-org/complex-project
  HEAD branch: master
  Remote branches:
    master                           tracked
    dev-branch                       tracked
    markdown-strip                   tracked
    issue-43                         new (next fetch will store in remotes/origin)
    issue-45                         new (next fetch will store in remotes/origin)
    refs/remotes/origin/issue-11     stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    dev-branch merges with remote dev-branch
    master     merges with remote master
  Local refs configured for 'git push':
    dev-branch                     pushes to dev-branch                     (up to date)
    markdown-strip                 pushes to markdown-strip                 (up to date)
    master                         pushes to master                         (up to date)

上面命令列出,当你在特定的分支执行git push 会自动推送到哪一个远程分支。哪些远程分支不在本地,哪些远程分支已经从服务器上移除。当执行git pull 时,哪些分支会自动合并。

添加远程仓库

运行git remote add <shortname> <url> 添加一个新的远程Git仓库,同时指定轻松引用的简写

$ git remote
origin
$ git remote add pb https://github.com/paulboone/ticgit
$ git remote -v
origin    https://github.com/schacon/ticgit (fetch)
origin    https://github.com/schacon/ticgit (push)
pb    https://github.com/paulboone/ticgit (fetch)
pb    https://github.com/paulboone/ticgit (push)

可以在命令中使用简写代替URL

$ git fetch pb
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done.
From https://github.com/paulboone/ticgit
 * [new branch]      master     -> pb/master
 * [new branch]      ticgit     -> pb/ticgit

抓取与拉取

从远程仓库获取数据,可以执行

$ git fetch [remote-name]

该命令会访问远程仓库,从中拉取所有你还没有的数据。

如果clone仓库,命令会自动将其添加为远程仓库,并默认以origin简写。

git fetch 命令会将数据拉取到本地仓库,它并不会自动合并或修改当前你的工作。当准备好时,你必须手动将其合并进入你的工作。

如果有一个分支设置为跟踪一个远程分支,可以使用git pull 命令自动的抓取然后合并远程分支到当前分支。

默认,git clone 将自动设置本地master分支跟踪克隆的远程仓库的master分支。因此,运行git pull 通常会从最初克隆的服务器抓取数据,并尝试合并到当前的所在分支。

推送

当想要分享项目时,必须将其推送到上游。命令格式

git push [remote-name] [branch-name]

将master分支推送到origin

git push origin master

只有当你有所克隆的服务器的写入权限,并且之前没有人推送时,上面命令才会生效。

当你和其他人在同一时间克隆,它们先推送到上游,然后你在推送,此时会被拒绝。

这时,你必须先将它们的工作拉取下拉,进行合并,才能进行推送。

远程仓库的移除与重命名

git remote rename可以修改一个远程仓库的简写名

$ git remote rename pb paul
$ git remote
origin
paul

如果因为某些原因需要移除一个远程仓库,比如,你已经从服务器上搬走了,或不再想使用特定镜像,或某个贡献者不再贡献了,可以将其移除

$ git remote rm paul
$ git remote
origin

results matching ""

    No results matching ""