如何理解远程“起源"获取.git/conf文件中的设置? [英] how to understand remote "origin" fetch setting in .git/conf file?

查看:91
本文介绍了如何理解远程“起源"获取.git/conf文件中的设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我学习pull&时,我在.git/config中看到了命令. 本文.

I saw the command in .git/config when I learn pull & request from this article.

fetch = +refs/pull/*/head:refs/pull/origin/*

我打开与此fetch = +refs/heads/*:refs/remotes/origin/*不同的配置文件.

I open my config file which is different as this fetch = +refs/heads/*:refs/remotes/origin/*.

在我修改fetch设置行后,请阅读并运行这些git命令:

After I modify fetch setting line as the article read and run those git commands:

git fetch origin
git checkout -b 1 pull/origin/1

config文件自动附加:

[branch "1"]
        remote = origin
        merge = refs/pull/1/head

有人可以解释fetch = ...的含义和差异设置吗?
为什么[branch "1"]将被自动附加?

Could someone can explain the means and difference settings of the fetch = ...?
Why [branch "1"] will be auto appended?

谢谢

推荐答案

这里有很多要解压的东西.让我们从头开始.

There's quite a lot to unpack here. Let's start from the beginning.

fetch设置的值称为 refspec ,一种特殊的Git语法,用于将 local 分支引用与远程分支映射.

The value of the fetch setting is what's called a refspec, a special Git syntax used to map local branch references with remote ones.

采用以下形式:

<source>:<destination>

对于fetch<source>是存在于远程存储库中的分支,而<destination>是它应映射到 local 中的分支. em>储存库.因此,例如:

In the case of fetch, <source> is a branch that exists on a remote repository, while <destination> is the branch it should map to in the local repository. So, for example:

fetch: +refs/heads/master:refs/remotes/origin/master

告诉Git将远程存储库中的master分支()映射到本地分支中的origin/master分支(目的地)

tells Git to map the master branch that exists in the remote repository (the source) to the origin/master branch in the local one (the destination).

+字符是可选的,表示即使需要合并提交,Git也应更新 destination 分支(由于您通常不直接提交给origin/master分支).

The + character is optional and means that Git should update the destination branch even if it would require a merge commit (which shouldn't happen since you normally don't commit directly to the origin/master branch).

现在您已经了解了refspec语法,让我们来谈谈从GitHub映射 pull请求分支.

Now that you know about the refspec syntax, let's talk about mapping the pull request branches from GitHub.

您知道,每个拉取请求都会在给定存储库中分配一个唯一的编号.但是,您可能不知道的是,这个数字最终变成了与拉取请求关联的分支的名称.

As you know, every pull request gets assigned a unique number within a given repository. What you may not know, however, is that this number ends up becoming the name of the branch associated to the pull request.

例如,编号为42的拉取请求将获得一个名为:

For example, a pull request with number 42 will get a branch reference named:

refs/pull/42

请注意如何在GitHub托管的存储库中名为pull的子目录中创建拉取请求分支.

Notice how pull request branches are created in a subdirectory called pull in the repository hosted on GitHub.

如果您想将GitHub上 remote 存储库中存在的 every 提取请求分支映射到具有相同名称的相应 local 分支,您会说:

If you wanted to map every pull request branch that exists in the remote repository on GitHub to a corresponding local branch with the same name, you would say:

fetch = +refs/pull/*/head:refs/pull/origin/*

其中,*是与任何名称匹配的通配符.使用此设置,下次您在本地存储库中执行git fetch时,Git将下载GitHub pull目录中存在的 all 分支引用,并在该目录下的本地存储库中创建相应的分支pull/origin.

where * is a wildcard character that matches any name. With this setting, next time you do git fetch in your local repository, Git will download all branch references that exist in the pull directory on GitHub and create corresponding branches in the local repository under the directory pull/origin.

这意味着,例如,对于我们的拉取请求42,映射变为:

This means that for our pull request 42, for example, the mapping becomes:

    GitHub        Your Repo
pull/42/head -> pull/origin/42

远程分支机构

请注意,pull/origin/*分支(尽管它们确实存在于本地存储库中)并不意味着您要提交.它们被称为 远程跟踪分支 ,并且Git用来从字面上跟踪远程存储库中存在的给定分支.

Remote Branches

Note that the pull/origin/* branches—while they do exist in your local repository—are not meant for you to commit to. They're called remote-tracking branches and are used by Git to literally keep track of a given branch that exist in a remote repository.

文档说得最好:

远程跟踪分支是对远程分支状态的引用.

Remote-tracking branches are references to the state of remote branches.

更具体地说:

将它们视为书签,以提醒您远程存储库中的分支是您最后一次连接到它们的位置.

Think of them as bookmarks, to remind you where the branches in your remote repositories were the last time you connected to them.

您不能直接提交到远程分支.如果要向前移动它,则必须首先创建一个 local 分支(即在本地存储库中仅 存在的分支)并将其与远程分支相关联.从那时起,每次在该本地分支上执行git pullgit push时,Git都会更新相应的远程跟踪分支.

You can't commit to a remote branch directly. If you want to move it forward, you must first create a local branch (i.e. one that exists only in your local repository) and associate it to the remote branch. From that point on, every time you do git pull or git push on that local branch, Git is going to update the corresponding remote tracking branch.

您可以使用一个命令来做到这一点:

You can do that with a single command:

git checkout -b 42 pull/origin/42

这将创建一个名为42 local 分支,并将其与远程跟踪分支pull/origin/42相关联,该分支又映射到GitHub上的pull/42分支.看起来是这样的:

This creates a local branch named 42 and associate it to the remote tracking branch pull/origin/42, which in turn is mapped to the pull/42 branch on GitHub. Here's how it looks like:

 Local      Remote        GitHub
  42 -> pull/origin/42 -> pull/42

local 分支和 remote 分支之间的关系在本地存储库的配置文件中表示为:

The relationship between local branch and remote branch is expressed in the local repository's configuration file as:

[branch "42"]
remote = origin
merge = refs/pull/42/head

remote 分支与物理上位于另一台机器(即GitHub上)上的分支之间的关系是通过使用我们在一开始查看的 refspec 语法表示的

The relationship between the remote branch and the branch that's physically on another machine (i.e. on GitHub) is expressed by using the refspec syntax that we looked at in the beginning.

这篇关于如何理解远程“起源"获取.git/conf文件中的设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆