git fetch的默认远程

时间:2013-05-30 13:55:40

标签: git git-fetch

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

git fetch

鉴于我在$GIT_DIR/config中定义了几个遥控器,从中取出远程是什么?

我试图从man page中找到答案,但这一点对我来说还不清楚。

此外:如何更改此默认遥控器而不进行本地分支跟踪?

4 个答案:

答案 0 :(得分:15)

如果您有多个远程存储库,并且未指定任何远程存储库名称,则默认情况下将使用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:“当没有指定远程时,默认情况下将使用原始远程,除非为当前分支配置了上游分支。”在这种情况下,您可以通过编辑origin中配置的分支的remote字段来更改上游分支以使用.git/config

答案 1 :(得分:3)

它将获取原始远程。这是你执行的第一个远程 GIT clone命令。

答案 2 :(得分:3)

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

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

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

所以代码是从这里列出的网址中提取的。

答案 3 :(得分:0)

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

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\" #"