美文网首页
remote 命令

remote 命令

作者: 一江碎月 | 来源:发表于2017-09-07 21:16 被阅读0次

为了便于管理,Git 要求每个远程仓库必须指定一个别名。remote 命令就用于管理仓库别名的命令

  1. 远程仓库指的是托管在网络上的项目仓库。

  2. 通过 clone 命令克隆下来的仓库,默认的别名是 origin。

  3. 主要涉及的操作有:增(add)、删(remove)、改(rename,set-url)、查(show , get-url)。

常用命令

命令 解释 选项
git remote add <name> <url> 添加新的远程库
git remote remove <name> 删除指定的仓库
git remote rename <old> <new> 将仓库别名重命名为 new
git remote set-url <name> <newurl> 将仓库的 url 修改为 newurl
git remote 列出所有的远程库 -v 表示查看每个库的 url
git remote show <name> 查看指定库的详细信息
git remote get-url <name> 获得指定仓库的 url

在操作时,将别名与 url 的关系理解为 Map 集合 —— 其中别名是 key,url 是 value。


添加

git remote add <name> <url>:添加一个远程库,同时为该远程库指定别名,使用别名指代远程库地址

git remote add images https://github.com/birdandcliff/images.git

以后可以使用 images 代替对应的url。

使用该命令可以将本地已有的文件与远程仓库关联,从而可以将本地已有的文件提交到远程仓库中。


删除

使用 git remote remove <name> 移除别名对应的仓库。

$ git remote
demo
newo

$ git remote remove newo
$ git remote
demo

修改

修改别名

使用 git remote rename <old> <name> 修改某个远程库的别名。

对远程库重命名后,对应的分支也会发生变化,将其中关于别名部分换成新别名。

$ git remote -v
demo    https://github.com/birdandcliff/seturl.git (fetch)
demo    https://github.com/birdandcliff/seturl.git (push)

$ git remote -v
demo    https://github.com/birdandcliff/seturl.git (fetch)
demo    https://github.com/birdandcliff/seturl.git (push)

$ git remote rename demo demo2
$ git remote -v
demo2   https://github.com/birdandcliff/seturl.git (fetch)
demo2   https://github.com/birdandcliff/seturl.git (push)

可以看出, url 没有发生变化,但对应的别名已经修改过了。

修改 url

使用 git remote set-url <name> <newurl> 将指定的远程仓库地址修改为 newurl。

$ git remote -v
demo    https://github.com/birdandcliff/gitdemo.git (fetch)
demo    https://github.com/birdandcliff/gitdemo.git (push)

$ git remote set-url demo https://github.com/birdandcliff/seturl.git
$ git remote -v
demo    https://github.com/birdandcliff/seturl.git (fetch)
demo    https://github.com/birdandcliff/seturl.git (push)

修改仓库地址后,可以将本地文件提交提交到新的 url 中。


查看

可以查看所有的别名,也可以查看所有的别名与 url,还能通过别名查看指定的 url。

  1. git remote 会列出每一个远程库的别名。可以使用 -v (verbose,详细)选项指定列出详细信息。如下:

  2. git remote show <name> 可查看某个远程库的详细信息

  3. git remote get-url <name> 根据 name 查看指定的 url

$ git remote
newo

$ git remote -v
newo    https://github.com/birdandcliff/seturl.git (fetch)
newo    https://github.com/birdandcliff/seturl.git (push)

$ git remote get-url newo
https://github.com/birdandcliff/seturl.git

$ git remote show newo
* remote newo
  Fetch URL: https://github.com/birdandcliff/seturl.git
  Push  URL: https://github.com/birdandcliff/seturl.git
  HEAD branch: master
  Remote branch:
    master 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)

remote 的配置信息

参考

使用 git remote add <别名> url 后,会在 .git/config 文件中添加如下信息:

[remote "demo"]
    url = https://github.com/birdandcliff/gitdemo.git
    fetch = +refs/heads/*:refs/remotes/demo/*

fetch 的格式由一个可选的 + 号和紧随其后的 <src>:<dst> 组成。其中 <src> 代表远程仓库中的引用;<dst> 是那些远程引用在本地所对应的位置。 + 号告诉 Git 即使在不能快进的情况下也要(强制)更新引用。

因此,Git 会获取 refs/heads/ 下面的所有引用,并将它写入到本地的 refs/remotes/demo/ 中。

  1. 可以对 fetch 进行手动修改。如将 fetch 行修改如下:

    fetch = +refs/heads/master:refs/remotes/demo/master
    

    那么使用 git fetch 时,就只会拉取远程仓库的 master。

  2. 分支信息并不一定要存储在 demo 目录下。可以在该目录下任意指定子目录。如:

    [remote "demo"]
        url = https://github.com/birdandcliff/gitdemo.git
        fetch = +refs/heads/re:refs/remotes/demo/devlocal
        fetch = +refs/heads/tra:refs/remotes/demo/xx/tra
    

    远程的 tra 分支存储的路径就在 demo/xx 目录下,而不是直接位于 demo 目录下。

    引用 tra 远程分支时,也需要写成 demo/xx/tra,不能直接写成 demo/tra 形式。如:

    $git branch -u demo/xx/tra
    Branch dev set up to track remote branch tra from demo.
    

相关文章

网友评论

      本文标题:remote 命令

      本文链接:https://www.haomeiwen.com/subject/tkrwjxtx.html