• Git 系列教程(8)- 远程仓库的使用


    查看远程仓库

    • 如果想查看你已经配置的远程仓库服务器,可以运行 git remote 命令,它会列出你指定的每一个远程服务器的名称
    • 如果是刚 clone 下来的自己的库,能看到 origin,这是 Git 起的默认名字
    $ 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 选项

    会显示需要读写远程仓库使用的 Git 的 URL和名称(origin)

    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 add <shortname> <url>

    添加一个新的远程 Git 仓库,同时指定一个名称

    polo@B-J5D1MD6R-2312 watermarker % git remote -v
    origin    git@gitee.com:poloyy/watermarker.git (fetch)
    origin    git@gitee.com:poloyy/watermarker.git (push)
    
    polo@B-J5D1MD6R-2312 watermarker % git  remote add test  git@gitee.com:testyy/waterm
    arker.git
    
    polo@B-J5D1MD6R-2312 watermarker % git remote -v
    origin    git@gitee.com:poloyy/watermarker.git (fetch)
    origin    git@gitee.com:poloyy/watermarker.git (push)
    test    git@gitee.com:testyy/watermarker.git (fetch)
    test    git@gitee.com:testyy/watermarker.git (push)

     可以使用 test 来代替整个 URL

    git fetch 小栗子

    可以运行 git fetch pb,拉取仓库数

    $ 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>
    • 这个命令会访问远程仓库,从中拉取所有你还没有的数据
    • 执行完成后,你将会拥有那个远程仓库中所有分支的引用,可以随时合并或查看

    重点

    git clone 命令克隆了一个仓库,Git  会自动将其添加为远程仓库并默认以 “origin” 为名称

    git fetch origin
    • 这样会抓取仓库里所有新 push 的内容
    • 但它只会将新内容下载到本地,并不会自动合并或修改当前内容,需要手动将新内容合并到本地内容中(git pull)

     

    git pull 初步认识

    • 默认情况下,git clone 命令会自动设置本地 master 分支跟踪 clone 下来的远程仓库的 master 分支(或其它名字的默认分支)
    • 运行 git pull 通常会从最初克隆的服务器上抓取数据并自动尝试合并到当前所在的分支

    推送到远程仓库

    语法格式

    git push <remote> <branch>

    将 master 分支的内容推送到 origin 服务器

    git push origin master

    remote 默认就是 origin,而 branch 默认是 master,所以等价写法就是

    git push

    重点

    如果在你推送前,远程仓库已经有新推送的内容,那么本地需要先拉取最新的内容并合并后,才能将本地的内容重新 push 到远程仓库

    # 一般的流程
    git fetch
    git pull
    git add .
    git commit -m "update"
    git push

    查看某个远程仓库

    git remote show <remote>

    可以查看远程仓库的更多信息

    % git  remote show origin
    * 远程 origin
      获取地址:git@gitee.com:poloyy/watermarker.git
      推送地址:git@gitee.com:poloyy/watermarker.git
      HEAD 分支:master
      远程分支:
        master 已跟踪
      为 'git pull' 配置的本地分支:
        master 与远程 master 合并
      为 'git push' 配置的本地引用:
        master 推送至 master (可快进)

    能获取到的信息

    • 远程仓库的 URL和名称(origin)
    • 跟踪分支的信息
    • 正处于 master 分支
    • 执行 git pull 可以拉取远程仓库的 master 内容,并和本地 master 分支的内容进行合
    • 执行 git push 可以将本地 master 分支内容推送到远程仓库的 master 分支上

    一个看到更多信息的栗子

    $ 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 remote rename <old> <new>

    小栗子

    $ git remote rename pb paul
    $ git remote
    origin
    paul

    同时会修改你所有远程跟踪的分支名字,之前引用 pb/master 的现在会引用 paul/master

    移除仓库的两种写法

    • git remote remove
    • git remote rm
    git remote remove paul
    $ git remote
    origin

    重点:一旦以这种方式删除了一个远程仓库,那么所有和这个远程仓库相关的远程跟踪分支以及配置信息也会一起被删除

  • 相关阅读:
    《白骨精学习法》 21世纪职场必备学习技巧
    英雄不问出处
    整理ArcSDE 安装过程出现问题以及解决方法系列
    ArcEngine9.1结合VS2005开发技巧2则
    推荐一界面控件DotNetBar(含附件)
    常用易忘记Oracle命令(待续)
    ArcSDE中间件技术的生命力(蔡晓兵)
    ArcSDE 的存储机制
    (收藏)ITPUB的ORACLE之常用FAQ V1.0
    ORA01691错误
  • 原文地址:https://www.cnblogs.com/poloyy/p/14773796.html
Copyright © 2020-2023  润新知