简体中文 ▾ 主题 ▾ 最新版本 ▾ git-remote 最后更新于 2.53.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 remotes),还会显示配置了哪些过滤器(如 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> 选项,将创建一个仅跟踪 <branch> 的引用规格(refspec),而不是默认跟踪 refs/remotes/<name>/ 命名空间下所有分支的全局引用规格。您可以指定多个 -t <branch> 来跟踪多个分支,而无需抓取所有分支。

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

当使用 --mirror=fetch 创建获取镜像时,引用将不会存储在 refs/remotes/ 命名空间中,而是将远程仓库 refs/ 下的所有内容直接镜像到本地仓库的 refs/ 中。此选项仅在纯仓库(bare repositories)中有意义,因为获取操作会覆盖任何本地提交。

当使用 --mirror=push 创建推送镜像时,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 必须已存在;如果不存在,则必须先获取它。

使用 <branch> 显式设置符号引用 refs/remotes/<name>/HEAD。例如,git remote set-head origin master 会将 refs/remotes/origin/HEAD 符号引用设置为 refs/remotes/origin/master。前提是 refs/remotes/origin/master 必须已存在;如果不存在,则必须先获取它。

set-branches

更改指定远程仓库所跟踪的分支列表。这可用于在远程仓库初始设置后,仅跟踪可用远程分支的一个子集。

指定的分支将被视为在 git remote add 命令行上使用 -t 选项指定的行为。

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

get-url

检索远程仓库的 URL。此处的 insteadOfpushInsteadOf 配置会被展开。默认情况下,只列出第一个 URL。

使用 --push,查询的是推送 URL 而非获取 URL。

使用 --all,将列出该远程仓库的所有 URL。

set-url

更改远程仓库的 URL。将远程仓库 <name> 中匹配正则表达式 <oldurl> 的第一个 URL 设置为 <newurl>(如果未指定 <oldurl>,则设置第一个 URL)。如果 <oldurl> 不匹配任何 URL,则会报错且不作任何更改。

使用 --push,操作的是推送 URL 而非获取 URL。

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

使用 --delete,不再更改现有 URL,而是删除远程仓库 <name> 中匹配正则表达式 <URL> 的所有 URL。尝试删除所有非推送 URL 会报错。

注意,尽管推送 URL 和获取 URL 可以分别设置,但它们必须指向同一个地方。推送到推送 URL 的内容,应该与立即从获取 URL 获取到的内容相同。如果您试图从一个地方(例如您的上游)获取,并推送到另一个地方(例如您的发布仓库),请使用两个独立的远程仓库。

show

提供有关远程仓库 <name> 的一些信息。

使用 -n 选项,不会首先通过 git ls-remote <name> 查询远程分支头;而是直接使用缓存信息。

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] 套件的一部分