【发布时间】:2013-05-26 03:30:14
【问题描述】:
如果我在不跟踪任何远程分支的本地分支上,我会发出命令
git fetch
鉴于我在$GIT_DIR/config 中定义了多个遥控器,从哪个遥控器完成提取?
我试图从man page 中找出答案,但这一点我不清楚。
另外:如何在不进行本地分支跟踪的情况下更改此默认远程?
【问题讨论】:
如果我在不跟踪任何远程分支的本地分支上,我会发出命令
git fetch
鉴于我在$GIT_DIR/config 中定义了多个遥控器,从哪个遥控器完成提取?
我试图从man page 中找出答案,但这一点我不清楚。
另外:如何在不进行本地分支跟踪的情况下更改此默认远程?
【问题讨论】:
它将获取原始遥控器。这是您执行的第一个遥控器
GIT clone 命令开启。
【讨论】:
git clone,而是在事后添加了遥控器(称为“origin”或其他)。
如果您有多个远程存储库,并且不指定任何远程存储库名称,则默认使用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 fetch 时,默认情况下将使用该遥控器已签出。
在您的项目文件夹中,当您在第一步初始化 git 时,会创建 .git 文件夹。
在这个文件夹中查找一个名为 config 的文件,它包含所有分支信息。 origin 被用作默认值。
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@github.com:project.git
所以代码是从这里列出的 url 中获取的。
【讨论】:
以下是一些别名,它们将提供可通过编程方式使用的字符串:
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\" #"
【讨论】: