This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
forked from progit/progit2-zh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Translated 10-git-internals/sections/refspec
- Loading branch information
Showing
1 changed file
with
24 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
[[_refspec]] | ||
=== The Refspec | ||
[[_引用表达式]] | ||
=== 引用表达式(Refspec) | ||
|
||
Throughout this book, we've used simple mappings from remote branches to local references, but they can be more complex. | ||
Suppose you add a remote like this: | ||
纵观全书,我们已经使用过一些诸如远程分支到本地引用的简单映射方式,但是还有更复杂的。 | ||
假设你添加了这样一个远程: | ||
|
||
[source,console] | ||
---- | ||
$ git remote add origin https://github.com/schacon/simplegit-progit | ||
---- | ||
|
||
It adds a section to your `.git/config` file, specifying the name of the remote (`origin`), the URL of the remote repository, and the refspec for fetching: | ||
上面的命令将在你的 `.git/config` 文件中添加一个小节,指定了远程的名称 (`origin`),远程仓库的URL地址,和用于获取操作的引用表达式(Refspec): | ||
|
||
[source,ini] | ||
---- | ||
|
@@ -18,11 +18,7 @@ It adds a section to your `.git/config` file, specifying the name of the remote | |
fetch = +refs/heads/*:refs/remotes/origin/* | ||
---- | ||
|
||
The format of the refspec is an optional `+`, followed by `<src>:<dst>`, where `<src>` is the pattern for references on the remote side and `<dst>` is where those references will be written locally. | ||
The `+` tells Git to update the reference even if it isn't a fast-forward. | ||
|
||
In the default case that is automatically written by a `git remote add` command, Git fetches all the references under `refs/heads/` on the server and writes them to `refs/remotes/origin/` locally. | ||
So, if there is a `master` branch on the server, you can access the log of that branch locally via | ||
引用表达式(refspec)的格式是由一个可选的 `+` 号和 `<src>:<dst>` 组成,这里 `<src>` 是远程上的引用格式, `<dst>` 是将要记录在本地的引用格式。可选的 `+` 号告诉 Git 即使不能快速演进,也要强制更新引用格式。默认情况下引用表达式(refspec)会由 `git remote add` 命令自动生成, Git 获取服务器中 `refs/heads/` 下面的所有引用,并将它写入到本地的 `refs/remotes/origin/`中。 所以,如果服务器上有一个 `master` 分支,即可在本地通过下面这种方式来访问分支记录: | ||
|
||
[source,console] | ||
---- | ||
|
@@ -31,26 +27,23 @@ $ git log remotes/origin/master | |
$ git log refs/remotes/origin/master | ||
---- | ||
|
||
They're all equivalent, because Git expands each of them to `refs/remotes/origin/master`. | ||
上面的三个命令作用相同,因为 Git 把它们都扩展成 `refs/remotes/origin/master`。 | ||
|
||
If you want Git instead to pull down only the `master` branch each time, and not every other branch on the remote server, you can change the fetch line to | ||
如果想让 Git 每次只拉取远程的 `master` 分支,而不是远程的所有分支,可以把 `fetch` 行修改为: | ||
|
||
[source] | ||
---- | ||
fetch = +refs/heads/master:refs/remotes/origin/master | ||
---- | ||
|
||
This is just the default refspec for `git fetch` for that remote. | ||
If you want to do something one time, you can specify the refspec on the command line, too. | ||
To pull the `master` branch on the remote down to `origin/mymaster` locally, you can run | ||
这只是针对远程的 `git fetch` 操作的默认引用表达式(refspec)。而如果只想做一次该操作,也可以在命令行上指定引用表达式(refspec)。拉取远程的 `master` 分支到本地的 `origin/mymaster` 分支,可以运行: | ||
|
||
[source,console] | ||
---- | ||
$ git fetch origin master:refs/remotes/origin/mymaster | ||
---- | ||
|
||
You can also specify multiple refspecs. | ||
On the command line, you can pull down several branches like so: | ||
也可以指定多个引用表达式(refspec)。像下面的命令行,可以一次拉取远程的多个分支: | ||
|
||
[source,console] | ||
---- | ||
|
@@ -61,11 +54,9 @@ From [email protected]:schacon/simplegit | |
* [new branch] topic -> origin/topic | ||
---- | ||
|
||
In this case, the master branch pull was rejected because it wasn't a fast-forward reference. | ||
You can override that by specifying the `+` in front of the refspec. | ||
在这个例子中,`master` 分支的拉取操作被拒绝,因为该分支不是一个可以快速演进的引用。可以通过在引用表达式(refspec)之前使用 `+` 号来强制更新。 | ||
|
||
You can also specify multiple refspecs for fetching in your configuration file. | ||
If you want to always fetch the master and experiment branches, add two lines: | ||
也可以在配置文件中指定多个引用表达式(refspec)。如果想在每次获取时都包括 `master` 和 `experiment` 分支,添加如下两行: | ||
|
||
[source,ini] | ||
---- | ||
|
@@ -75,15 +66,15 @@ If you want to always fetch the master and experiment branches, add two lines: | |
fetch = +refs/heads/experiment:refs/remotes/origin/experiment | ||
---- | ||
|
||
You can't use partial globs in the pattern, so this would be invalid: | ||
注意这里不能使用部分通配符,所以下面这样就是不合法的: | ||
|
||
[source] | ||
---- | ||
fetch = +refs/heads/qa*:refs/remotes/origin/qa* | ||
---- | ||
|
||
However, you can use namespaces (or directories) to accomplish something like that. | ||
If you have a QA team that pushes a series of branches, and you want to get the master branch and any of the QA team's branches but nothing else, you can use a config section like this: | ||
但可以使用命名空间(或文件路径)来达到这个目的。 | ||
如您有一个QA团队,他们推送一系列分支。您每次只想获取 `master` 分支和QA团队的所有分支,可以使用如下配置段落: | ||
|
||
[source,ini] | ||
---- | ||
|
@@ -93,22 +84,19 @@ If you have a QA team that pushes a series of branches, and you want to get the | |
fetch = +refs/heads/qa/*:refs/remotes/origin/qa/* | ||
---- | ||
|
||
If you have a complex workflow process that has a QA team pushing branches, developers pushing branches, and integration teams pushing and collaborating on remote branches, you can namespace them easily this way. | ||
|
||
[[_pushing_refspecs]] | ||
==== Pushing Refspecs | ||
如果工作流很复杂,有QA团队推送的分支、开发人员推送的分支、还有集成团队推送并且在远程分支上协作,可以为他们创建各自的命名空间解决类似问题。 | ||
|
||
It's nice that you can fetch namespaced references that way, but how does the QA team get their branches into a `qa/` namespace in the first place? | ||
You accomplish that by using refspecs to push. | ||
[[_推送_引用表达式]] | ||
==== 推送引用表达式(refspec) | ||
|
||
If the QA team wants to push their `master` branch to `qa/master` on the remote server, they can run | ||
采用命名空间的方式来获取分支是个好主意,但QA组成员如何将他们的分支开始推送到 `qa/` 空间里面的呢?答案是通过引用表达式(refspec)的推送来完成。如果QA团队想把他们的 `master` 分支推送到远程服务器的 `qa/master` 分支上,可以运行: | ||
|
||
[source,console] | ||
---- | ||
$ git push origin master:refs/heads/qa/master | ||
---- | ||
|
||
If they want Git to do that automatically each time they run `git push origin`, they can add a `push` value to their config file: | ||
如果他们希望 `Git` 每次运行 `git push origin` 时都如上这样推送,他们可以在配置文件中添加一个 `push` 值: | ||
|
||
[source,ini] | ||
---- | ||
|
@@ -118,15 +106,16 @@ If they want Git to do that automatically each time they run `git push origin`, | |
push = refs/heads/master:refs/heads/qa/master | ||
---- | ||
|
||
Again, this will cause a `git push origin` to push the local `master` branch to the remote `qa/master` branch by default. | ||
同样的,这就会让 `git push origin` 默认就把本地的 `master` 分支推送到远程的 `qa/master` 分支上。 | ||
|
||
==== Deleting References | ||
==== 删除引用 | ||
|
||
You can also use the refspec to delete references from the remote server by running something like this: | ||
您也可以使用引用表达式(refspec)来删除远程服务器的引用,运行如下命令: | ||
|
||
[source,console] | ||
---- | ||
$ git push origin :topic | ||
---- | ||
|
||
Because the refspec is `<src>:<dst>`, by leaving off the `<src>` part, this basically says to make the topic branch on the remote nothing, which deletes it. | ||
因为引用表达式(refspec)的格式是 `<src>:<dst>`, 所以把 `<src>` 留空的意思就是把远程的 `topic` 分支定义为空值,也就是删除它。 |