Skip to content

Commit a2f4b94

Browse files
geccegoldsharp
authored andcommitted
translate 7.13
1 parent 70dfc76 commit a2f4b94

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

book/07-git-tools/sections/replace.asc

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
[[_replace]]
2-
=== Replace
2+
=== 替换
33

4-
Git's objects are unchangable, but it does provide an interesting way to pretend to replace objects in it's database with other objects.
4+
Git对象是不可改变的,但它提供一种有趣的方式来用其他对象假替换数据库中的数个Git对象。
55

6-
The `replace` command lets you specify an object in Git and say "every time you see this, pretend it's this other thing". This is most commonly useful for replacing one commit in your history with another one.
76

8-
For example, let's say you have a huge code history and want to split your repository into one short history for new developers and one much longer and larger history for people interested in data mining. You can graft one history onto the other by `replace`ing the earliest commit in the new line with the latest commit on the older one. This is nice because it means that you don't actually have to rewrite every commit in the new history, as you would normally have to do to join them together (because the parentage effects the SHAs).
7+
`replace`命令可以让你在Git中指定一个对象并可以声称“每次你遇到这个Git对象时,假装它是其他的东西”。在你的历史中替换一个已有的提交时,这是一个最常用的方法。
98

10-
Let's try this out. Let's take an existing repository, split it into two repositories, one recent and one historical, and then we'll see how we can recombine them without modifying the recent repositories SHA values via `replace`.
119

12-
We'll use a simple repository with five simple commits:
10+
例如,你有一个大型的代码历史并想把自己的仓库分成一个短的历史和一个更大更长久的历史,短历史供新的开发者使用,后者给喜欢数据挖掘的人使用。你可以通过最新提交来“替换”早期提交的方式来将一个历史移植到其他历史上。这意味着你不用在新历史中真正替换每一个提交,(历史来源会影响SHA的值)你可以加入他们。
11+
12+
让我们来试试吧!首先获取一个已经存在的仓库,并将其分成两个仓库,一个是最近的仓库,一个是历史版本的仓库。然我们将见证在不更改仓库SHA值的情况下通过`replace`命令来合并他们。
13+
14+
我们将使用一个拥有5个提交的简单仓库:
1315

1416
[source,console]
1517
----
@@ -21,11 +23,11 @@ c6e1e95 fourth commit
2123
c1822cf first commit
2224
----
2325

24-
We want to break this up into two lines of history. One line goes from commit one to commit four - that will be the historical one. The second line will just be commits four and five - that will be the recent history.
26+
我们想将其分成拆分成两行历史。第一行作为历史版本的仓库包括第一个到第四个提交,第二行作为最近的仓库包括第四、五个提交。
2527

2628
image::images/replace1.png[]
2729

28-
Well, creating the historical history is easy, we can just put a branch in the history and then push that branch to the master branch of a new remote repository.
30+
创建历史版本的历史很容易,我们可以只将一个分支放入历史中,然后将这个分支推送到一个新的远程仓库的master分支。
2931

3032
[source,console]
3133
----
@@ -40,7 +42,7 @@ c1822cf first commit
4042

4143
image::images/replace2.png[]
4244

43-
Now we can push the new `history` branch to the `master` branch of our new repository:
45+
现在我们可以把这个新的`history`分支推送到我们新仓库的`master`分支:
4446

4547
[source,console]
4648
----
@@ -56,7 +58,7 @@ To [email protected]:schacon/project-history.git
5658
* [new branch] history -> master
5759
----
5860

59-
OK, so our history is published. Now the harder part is truncating our recent history down so it's smaller. We need an overlap so we can replace a commit in one with an equivalent commit in the other, so we're going to truncate this to just commits four and five (so commit four overlaps).
61+
这样一来,我们的历史就发布了。稍难的部分则是截断我们最近的历史。我们需要一个重叠以便于用一个相等的提交来替换另一个提交,这样一来,我们将截断到第四、五个提交。
6062

6163
[source,console]
6264
----
@@ -68,10 +70,10 @@ c6e1e95 (history) fourth commit
6870
c1822cf first commit
6971
----
7072

71-
It's useful in this case to create a base commit that has instructions on how to expand the history, so other developers know what to do if they hit the first commit in the truncated history and need more. So, what we're going to do is create an initial commit object as our base point with instructions, then rebase the remaining commits (four and five) on top of it.
72-
73-
To do that, we need to choose a point to split at, which for us is the third commit, which is `9c68fdc` in SHA-speak. So, our base commit will be based off of that tree. We can create our base commit using the `commit-tree` command, which just takes a tree and will give us a brand new, parentless commit object SHA back.
73+
在这个案例中,创建一个基础提交很有用,这个提交拥有如何扩展历史的命令。这样一来,如果其他的开发者在截断的历史中命中第一个提交或需要更多提交时就知晓做什么了。
74+
因此,接下来我们要做的是用命令创建一个最初的提交对象 ,然后在其上面变基剩下的提交(第四、五个提交)。
7475

76+
为了这么做,我们需要选择一个点去拆分,对于我们而言是第三个提交(SHA是`9c68fdc`),因此我们的提交将基于此提交树。我们可以使用`commit-tree`命令来创建基础提交,这样我们就有了一个树,并返回一个全新的、无父节点的SHA提交对象。
7577
[source,console]
7678
----
7779
$ echo 'get history from blah blah blah' | git commit-tree 9c68fdc^{tree}
@@ -80,12 +82,12 @@ $ echo 'get history from blah blah blah' | git commit-tree 9c68fdc^{tree}
8082

8183
[NOTE]
8284
=====
83-
The `commit-tree` command is one of a set of commands that are commonly referred to as 'plumbing' commands. These are commands that are not generally meant to be used directly, but instead are used by **other** Git commands to do smaller jobs. On occasions when we're doing weirder things like this, they allow us to do really low-level things but are not meant for daily use. You can read more about plumbing commands in <<_plumbing_porcelain>>
85+
`commit-tree`命令属于底层指令。有许多指令并非直接使用,而需通过其他的Git命令替换使用。有时当我们做一些像这样的奇怪事情时,Git允许我们偶尔使用这些底层的命令。想了解更多关于底层命令的内容参见<<_plumbing_porcelain>>
8486
=====
8587

8688
image::images/replace3.png[]
8789

88-
OK, so now that we have a base commit, we can rebase the rest of our history on top of that with `git rebase --onto`. The `--onto` argument will be the SHA we just got back from `commit-tree` and the rebase point will be the third commit (the parent of the first commit we want to keep, `9c68fdc`):
90+
现在我们已经有一个基础提交了,我们可以通过`git rebase --onto`命令来讲其他的历史变基到基础提交之上。`--onto`参数是刚才`commit-tree`命令返回的SHA值,变基点则在第三个提交:
8991

9092
[source,console]
9193
----
@@ -97,10 +99,10 @@ Applying: fifth commit
9799

98100
image::images/replace4.png[]
99101

100-
OK, so now we've re-written our recent history on top of a throw away base commit that now has instructions in it on how to reconstitute the entire history if we wanted to. We can push that new history to a new project and now when people clone that repository, they will only see the most recent two commits and then a base commit with instructions.
102+
我们已经用基础提交重写了最近的历史,基础提交包括如何重新组成整个历史的命令。我们可以将新历史提交到新项目中,当其他人克隆这个仓库时,他们仅能看到最近两次提交以及一个包含命令的的基础提交。
103+
104+
现在我们将以其他人的身份来克隆这个项目。为了在克隆截断后的仓库后得到历史数据,需要添加第二个远程的历史版本库并取回。
101105

102-
Let's now switch roles to someone cloning the project for the first time who wants the entire history.
103-
To get the history data after cloning this truncated repository, one would have to add a second remote for the historical repository and fetch:
104106

105107
[source,console]
106108
----
@@ -118,7 +120,7 @@ From https://github.com/schacon/project-history
118120
* [new branch] master -> project-history/master
119121
----
120122

121-
Now the collaborator would have their recent commits in the `master` branch and the historical commits in the `project-history/master` branch.
123+
现在,合作者在`master`分支中拥有他们最近的提交并且在`project-history/master`分支中拥有过去的提交。
122124

123125
[source,console]
124126
----
@@ -134,14 +136,14 @@ c6e1e95 fourth commit
134136
c1822cf first commit
135137
----
136138

137-
To combine them, you can simply call `git replace` with the commit you want to replace and then the commit you want to replace it with. So we want to replace the "fourth" commit in the master branch with the "fourth" commit in the `project-history/master` branch:
139+
为了合并他们,你可以使用`git replace`命令加上你想替换的提交信息来进行替换。这样一来,我么就可以将`project-history/master`分支中的第四个提交替换为master分支中的第四次提交。
138140

139141
[source,console]
140142
----
141143
$ git replace 81a708d c6e1e95
142144
----
143145

144-
Now, if you look at the history of the `master` branch, it appears to look like this:
146+
现在,查看`master`分支中的历史信息,显示如下:
145147

146148
[source,console]
147149
----
@@ -153,11 +155,11 @@ e146b5f fifth commit
153155
c1822cf first commit
154156
----
155157

156-
Cool, right? Without having to change all the SHAs upstream, we were able to replace one commit in our history with an entirely different commit and all the normal tools (`bisect`, `blame`, etc) will work how we would expect them to.
158+
不用改变上游的SHA我们就能用一个提交来替换历史中的所有不同的提交,并且所有的工具(bisect,blame等)也都奏效。
157159

158160
image::images/replace5.png[]
159161

160-
Interestingly, it still shows `81a708d` as the SHA, even though it's actually using the `c6e1e95` commit data that we replaced it with. Even if you run a command like `cat-file`, it will show you the replaced data:
162+
有趣的是,即使是使用了`c6e1e95`提交数据来进行替换,它的SHA仍显示为`81a708d`。即使你运行了`cat-file`命令,它仍会显示你替换的数据:
161163

162164
[source,console]
163165
----
@@ -170,9 +172,9 @@ committer Scott Chacon <[email protected]> 1268712581 -0700
170172
fourth commit
171173
----
172174

173-
Remember that the actual parent of `81a708d` was our placeholder commit (`622e88e`), not `9c68fdce` as it states here.
175+
请记住,`81a708d`真正的父提交是`622e882`提交,而非呈现的`9c68fdce`提交。
174176

175-
Another interesting thing is that this data is kept in our references:
177+
另一个有趣的事情是数据将会以以下引用显示:
176178

177179
[source,console]
178180
----
@@ -184,4 +186,4 @@ e146b5f14e79d4935160c0e83fb9ebe526b8da0d commit refs/remotes/origin/master
184186
c6e1e95051d41771a649f3145423f8809d1a74d4 commit refs/replace/81a708dd0e167a3f691541c7a6463343bc457040
185187
----
186188

187-
This means that it's easy to share our replacement with others, because we can push this to our server and other people can easily download it. This is not that helpful in the history grafting scenario we've gone over here (since everyone would be downloading both histories anyhow, so why separate them?) but it can be useful in other circumstances.
189+
这意味着可以轻而易举的和其他人分享我们的替换,正因为我们可以将替换推送到服务器中,其他人才可以下载到。也许在历史移植情况下不是很有用(既然每个人都能轻松下载到,为何还要拆分他们呢?),但是在其他情况下这种替换方式仍有其广泛的用途。

0 commit comments

Comments
 (0)