【问题标题】:Default remote for git fetchgit fetch 的默认远程
【发布时间】:2013-05-26 03:30:14
【问题描述】:

如果我在跟踪任何远程分支的本地分支上,我会发出命令

git fetch

鉴于我在$GIT_DIR/config 中定义了多个遥控器,从哪个遥控器完成提取?

我试图从man page 中找出答案,但这一点我不清楚。

另外:如何在不进行本地分支跟踪的情况下更改此默认远程?

【问题讨论】:

    标签: git git-fetch


    【解决方案1】:

    它将获取原始遥控器。这是您执行的第一个遥控器 GIT clone 命令开启。

    【讨论】:

    • 你可能从来没有在 repo 上执行过git clone,而是在事后添加了遥控器(称为“origin”或其他)。
    【解决方案2】:

    如果您有多个远程存储库,并且不指定任何远程存储库名称,则默认使用origin。如果没有名为 origin 的远程仓库,则会报错

    fatal: No remote repository specified.  Please, specify either a URL or a
    remote name from which new revisions should be fetched.
    

    另外:如何在不进行本地分支跟踪的情况下更改此默认远程?

    您可以将该存储库名称重命名为“origin”以使其成为默认值。

    小心:如果当前分支已经在不同的远程指定了上游,这将不起作用。
    来自git help fetch

    当没有指定远程时,默认使用源远程,除非为当前分支配置了上游分支。

    在这种情况下,您可以通过编辑在.git/config 中配置的分支的remote 字段,将上游分支更改为使用origin

    【讨论】:

    • 把遥控器的名字改成origin没有别的办法吗?
    • 让 git 假设“origin”为默认远程有点不灵活。我总是将我的遥控器重命名为更有意义,例如“github”、“internal”等。是的,当“origin”遥控器不存在时,git 会尖叫“致命:未指定远程存储库。”
    • @KlasMellbourn:您可以将跟踪分支的遥控器设置为使用与origin 不同的遥控器,然后当您使用其中一个分支运行git fetch 时,默认情况下将使用该遥控器已签出。
    【解决方案3】:

    在您的项目文件夹中,当您在第一步初始化 git 时,会创建 .git 文件夹。

    在这个文件夹中查找一个名为 config 的文件,它包含所有分支信息。 origin 被用作默认值。

      [remote "origin"]
          fetch = +refs/heads/*:refs/remotes/origin/*
          url = git@github.com:project.git
    

    所以代码是从这里列出的 url 中获取的。

    【讨论】:

      【解决方案4】:

      以下是一些别名,它们将提供可通过编程方式使用的字符串:

      branch-name = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
      branch-remote-fetch = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".remote || echo origin #"
      branch-remote-push  = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".pushRemote || git config remote.pushDefault || git branch-remote-fetch #"
      branch-url-fetch = !"remote=$(git branch-remote-fetch \"$1\") && git remote get-url        \"$remote\" #"  # cognizant of insteadOf
      branch-url-push  = !"remote=$(git branch-remote-push  \"$1\") && git remote get-url --push \"$remote\" #"  # cognizant of pushInsteadOf
      

      如果您想找到另一个分支的远程,请将上面的branch-name 替换为允许传递单个参数:

      branch-current = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
      branch-names = !"[ -z \"$1\" ] && git branch-current 2>/dev/null || git branch --format='%(refname:short)' --contains \"${1:-HEAD}\" #"  # https://stackoverflow.com/a/19585361/5353461
      branch-name = !"br=$(git branch-names \"$1\") && case \"$br\" in *$'\\n'*) printf \"Multiple branches:\\n%s\" \"$br\">&2; exit 1;; esac; echo \"$br\" #"
      

      【讨论】:

        猜你喜欢
        • 2016-09-05
        • 1970-01-01
        • 2022-01-09
        • 2017-03-07
        • 2019-07-29
        • 2013-07-22
        • 2011-02-24
        • 2011-06-18
        • 1970-01-01
        相关资源
        最近更新 更多