了解.git / config的'remote'和'branch'部分 [英] Understanding .git/config's 'remote' and 'branch' sections

查看:637
本文介绍了了解.git / config的'remote'和'branch'部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的 .git / config的远程分支部分的内容 $ b


  [远程原产地] 
url = https://EvanAad@bitbucket.org/EvanAad/bitbucketstationlocations.git
fetch = + refs / heads / *:refs / remotes / origin / *
[branchmaster]
remote = origin
merge = refs / heads / master


这些部分的内容的含义和目的是什么,特别是 fetch merge 小节?这个信息是如何被Git用来指导它的操作的? 解决方案

它叫做refspec。
它是git用于与远程服务器交谈并将本地分支映射到远程分支机构的机器。

Refspecs



refspec将本地存储库中的分支映射到远程存储库中的分支。

这使得可以使用本地Git命令并配置一些高级的git push和git fetch行为。


$ b 一个refspec指定为 [+]< src>:< DST> < src> 参数是本地存储库中的源分支,而< dst> 参数是目标分支。

可选的 + 标志用于强制远程存储库执行非快进更新


Refspecs可以与git push命令一起使用,为远程分支提供不同的名称。例如,下面的命令像普通的git push一样将主分支推送到原始远程回购,但它使用qa-master作为原始回购中分支的名称。这对于需要将他们自己的分支推送到远程仓库的QA团队非常有用。

  git push origin master:refs / head / qa-master 

通过添加几行到Git配置文件,您可以使用refspecs改变git fetch的行为。



默认情况下, git fetch 在远程仓库中分支。原因是 .git / config 文件中的以下部分:

  [remoteorigin] 
url = https://git@github.com:mary / example-repo.git
fetch = + refs / heads / *:refs / remotes / origin / *

fetch 行告诉git fetch到从原始库存中下载所有分支

但是,某些工作流程并不需要所有这些分支。例如,许多持续集成工作流程只关心主分支。要仅提取主分支,请将获取行更改为匹配以下内容:

  [remoteorigin] 
url = https://git@github.com:mary / example-repo.git
fetch = + refs / heads / master:refs / remotes / origin / master

你也可以用类似的方式配置git push。例如,如果您想要始终将原始分支推送到原始远程中的qa-master(如上所述),则可以将配置文件更改为:

  [remoteorigin] 
url = https://git@github.com:mary / example-repo.git
fetch = + refs / heads / master :refs / remotes / origin / master
push = refs / heads / master:refs / heads / qa-master

Refspecs可让您完全控制各种Git命令如何在存储库之间传输分支

它们允许您从本地存储库 删除分支,获取/推送到不同名称的分支,并配置git push和git fetch以仅使用您想要的分支。


Here's the contents of the remote and branch sections of my .git/config file.

[remote "origin"]  
    url = https://EvanAad@bitbucket.org/EvanAad/bitbucketstationlocations.git  
    fetch = +refs/heads/*:refs/remotes/origin/*  
[branch "master"]  
    remote = origin  
    merge = refs/heads/master

What is the meaning and purpose of the contents of these sections, in particular the fetch and merge subsections? How is this information used by Git to guide its operation?

解决方案

Its called refspec. Its the mechmism that git is using to "talk" to the remote server and to map local branches to remote branches.

Refspecs

A refspec maps a branch in the local repository to a branch in a remote repository.
This makes it possible to manage remote branches using local Git commands and to configure some advanced git push and git fetch behavior.

A refspec is specified as [+]<src>:<dst>. The <src> parameter is the source branch in the local repository, and the <dst> parameter is the destination branch in the remote repository.
The optional + sign is for forcing the remote repository to perform a non-fast-forward update.

Refspecs can be used with the git push command to give a different name to the remote branch. For example, the following command pushes the master branch to the origin remote repo like an ordinary git push, but it uses qa-master as the name for the branch in the origin repo. This is useful for QA teams that need to push their own branches to a remote repo.

git push origin master:refs/heads/qa-master

By adding a few lines to the Git configuration file, you can use refspecs to alter the behavior of git fetch.

By default, git fetch fetches all of the branches in the remote repository. The reason for this is the following section of the .git/config file:

[remote "origin"]
    url = https://git@github.com:mary/example-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

The fetch line tells git fetch to download all of the branches from the origin repo.
But, some workflows don’t need all of them. For example, many continuous integration workflows only care about the master branch. To fetch only the master branch, change the fetch line to match the following:

[remote "origin"]
    url = https://git@github.com:mary/example-repo.git
    fetch = +refs/heads/master:refs/remotes/origin/master

You can also configure git push in a similar manner. For instance, if you want to always push the master branch to qa-master in the origin remote (as we did above), you would change the config file to:

[remote "origin"]
    url = https://git@github.com:mary/example-repo.git
    fetch = +refs/heads/master:refs/remotes/origin/master
    push = refs/heads/master:refs/heads/qa-master

Refspecs give you complete control over how various Git commands transfer branches between repositories.

They let you rename and delete branches from your local repository, fetch/push to branches with different names, and configure git push and git fetch to work with only the branches that you want.

这篇关于了解.git / config的'remote'和'branch'部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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