简体中文 ▾ 主题 ▾ 最新版本 ▾ git-remote 最后更新于 2.45.0

名称

git-remote - 管理跟踪的仓库集

概要

git remote [-v | --verbose]
git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>
git remote rename [--[no-]progress] <old> <new>
git remote remove <name>
git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
git remote set-branches [--add] <name> <branch>…​
git remote get-url [--push] [--all] <name>
git remote set-url [--push] <name> <newurl> [<oldurl>]
git remote set-url --add [--push] <name> <newurl>
git remote set-url --delete [--push] <name> <URL>
git remote [-v | --verbose] show [-n] <name>…​
git remote prune [-n | --dry-run] <name>…​
git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)…​]

描述

管理你跟踪其分支的仓库("远程")的集合。

选项

-v
--verbose

稍微详细一些,并在名称后显示远程 URL。对于 promisor 远程,还会显示配置了哪些过滤器(blob:none 等)。注意:这必须放在 remote 和子命令之间。

命令

不带参数时,显示现有远程列表。有几个子命令可用于对远程执行操作。

add

为 <URL> 处的仓库添加一个名为 <name> 的远程。然后可以使用 git fetch <name> 命令来创建和更新远程跟踪分支 <name>/<branch>

使用 -f 选项时,在设置完远程信息后立即运行 git fetch <name>

使用 --tags 选项时,git fetch <name> 会从远程仓库导入所有标签。

使用 --no-tags 选项时,git fetch <name> 不会从远程仓库导入标签。

默认情况下,仅导入已获取分支上的标签(请参阅 git-fetch[1])。

使用 -t <branch> 选项,而不是默认的 glob refspec 来跟踪 refs/remotes/<name>/ 命名空间下的所有分支,而是创建一个仅跟踪 <branch> 的 refspec。你可以给出多个 -t <branch> 来跟踪多个分支,而无需获取所有分支。

使用 -m <master> 选项,将设置一个符号引用 refs/remotes/<name>/HEAD 来指向远程的 <master> 分支。另请参阅 set-head 命令。

当使用 --mirror=fetch 创建一个 fetch mirror 时,引用不会存储在 refs/remotes/ 命名空间中,而是远程的 refs/ 下的任何内容都会直接镜像到本地仓库的 refs/ 中。此选项仅在裸仓库中有意义,因为 fetch 会覆盖任何本地提交。

当使用 --mirror=push 创建一个 push mirror 时,git push 将始终表现得好像传递了 --mirror

rename

将名为 <old> 的远程重命名为 <new>。所有远程跟踪分支和远程的配置设置都会被更新。

如果 <old> 和 <new> 相同,并且 <old> 是 $GIT_DIR/remotes$GIT_DIR/branches 下的一个文件,则该远程将被转换为配置文件格式。

remove
rm

删除名为 <name> 的远程。所有远程跟踪分支和远程的配置设置都会被删除。

set-head

设置或删除指定远程的默认分支(即符号引用 refs/remotes/<name>/HEAD 的目标)。远程拥有默认分支不是必需的,但它允许用远程名称本身来代替特定的分支。例如,如果 origin 的默认分支设置为 master,那么可以在任何原本需要指定 origin/master 的地方指定 origin

使用 -d--delete,将删除符号引用 refs/remotes/<name>/HEAD

使用 -a--auto,将查询远程以确定其 HEAD,然后将符号引用 refs/remotes/<name>/HEAD 设置为同一分支。例如,如果远程 HEAD 指向 next,那么 git remote set-head origin -a 将把符号引用 refs/remotes/origin/HEAD 设置为 refs/remotes/origin/next。只有当 refs/remotes/origin/next 已存在时,此操作才有效;否则,必须先进行 fetch。

使用 <branch> 显式设置符号引用 refs/remotes/<name>/HEAD。例如,git remote set-head origin master 将把符号引用 refs/remotes/origin/HEAD 设置为 refs/remotes/origin/master。只有当 refs/remotes/origin/master 已存在时,此操作才有效;否则,必须先进行 fetch。

set-branches

更改指定远程跟踪的分支列表。这可用于在远程初始设置后跟踪远程可用分支的子集。

指定的受跟踪分支将按照在 git remote add 命令行的 -t 选项中所指定的方式进行解释。

使用 --add 时,不是替换当前跟踪的分支列表,而是将新分支添加到该列表中。

get-url

检索远程的 URL。在此会展开 insteadOfpushInsteadOf 的配置。默认情况下,仅列出第一个 URL。

使用 --push 时,查询的是 push URL 而不是 fetch URL。

使用 --all 时,将列出远程的所有 URL。

set-url

更改远程的 URL。将远程 <name> 中第一个匹配正则表达式 <oldurl> 的 URL(如果未给出 <oldurl>,则为第一个 URL)更改为 <newurl>。如果 <oldurl> 不匹配任何 URL,则会发生错误且不进行任何更改。

使用 --push 时,将操作 push URL 而不是 fetch URL。

使用 --add 时,不是更改现有 URL,而是添加新 URL。

使用 --delete 时,不是更改现有 URL,而是删除远程 <name> 中所有匹配正则表达式 <URL> 的 URL。尝试删除所有非 push URL 会导致错误。

请注意,push URL 和 fetch URL 尽管可以设置不同,但它们仍然必须指向同一个地方。你推送到 push URL 的内容应该与你在立即从 fetch URL 获取时看到的内容一致。如果你试图从一个地方(例如你的上游)获取,然后推送到另一个地方(例如你的发布仓库),请使用两个独立的远程。

show

提供关于远程 <name> 的一些信息。

使用 -n 选项时,不会先使用 git ls-remote <name> 查询远程 HEAD;而是使用缓存的信息。

prune

删除与 <name> 相关的陈旧引用。默认情况下,会删除 <name> 下的陈旧远程跟踪分支,但根据全局配置和远程的配置,我们甚至可能会修剪未推送到那里的本地标签。相当于 git fetch --prune <name>,只是不会获取新的引用。

请参阅 git-fetch[1] 的 PRUNING 部分,了解根据各种配置会修剪哪些内容。

使用 --dry-run 选项,报告将要被修剪的分支,但实际上不进行修剪。

update

根据 remotes.<group> 的定义,获取仓库中远程或远程组的更新。如果命令行中没有指定组或远程,则使用配置参数 remotes.default;如果 remotes.default 未定义,则会更新所有未设置 remote.<name>.skipDefaultUpdate 为 true 的远程。(请参阅 git-config[1])。

使用 --prune 选项时,会对所有更新的远程执行修剪操作。

讨论

远程配置是通过 remote.origin.urlremote.origin.fetch 配置变量实现的。(请参阅 git-config[1])。

退出状态

成功时,退出状态为 0

addrenameremove 等子命令找不到指定的远程时,退出状态为 2。当远程已存在时,退出状态为 3

发生任何其他错误时,退出状态可能是任何其他非零值。

示例

  • 添加一个新远程,获取并检出其中的一个分支

    $ git remote
    origin
    $ git branch -r
      origin/HEAD -> origin/master
      origin/master
    $ git remote add staging git://git.kernel.org/.../gregkh/staging.git
    $ git remote
    origin
    staging
    $ git fetch staging
    ...
    From git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
     * [new branch]      master     -> staging/master
     * [new branch]      staging-linus -> staging/staging-linus
     * [new branch]      staging-next -> staging/staging-next
    $ git branch -r
      origin/HEAD -> origin/master
      origin/master
      staging/master
      staging/staging-linus
      staging/staging-next
    $ git switch -c staging staging/master
    ...
  • 模仿 git clone 但只跟踪选定的分支

    $ mkdir project.git
    $ cd project.git
    $ git init
    $ git remote add -f -t master -m master origin git://example.com/git.git/
    $ git merge origin

GIT

Git[1] 套件的一部分