start edit history.txt
[gitmagic.git] / vi / history.txt
blob70c13e57c654e3adffc755d26fbab3112a3417b1
1 == Lessons of History ==
3 A consequence of Git's distributed nature is that history can be edited
4 easily. Nhưng nếu bạn xáo trộn quá khứ, hãy cẩn thận: only rewrite that part of
5 history which you alone possess. Just as nations forever argue over who
6 committed what atrocity, if someone else has a clone whose version of history
7 differs to yours, you will have trouble reconciling when your trees interact.
9 Một số nhà phát triển phần mềm quả quyết rằng lịch sử có thể be immutable, warts and all.
10 Others feel trees should be made presentable before they are unleashed in
11 public. Git accommodates both viewpoints. Giống như việc nhân bản, tạo nhánh và hòa trộn,
12 viết lại lịch sử đơn giản chỉ là một quyền mà Git trao cho bạn. It is up to you
13 to use it wisely.
15 === I Stand Corrected ===
17 Bạn vừa mới commit, nhưng lại ước mình đã gõ những dòng chú thích có nội dung khác phải không? Thế thì hãy chạy:
19  $ git commit --amend
21 để thay đổi chú thích cuối cùng. Bạn giật mình vì quên thêm các tệp tin vào? Chạy lệnh *git add* để
22 thêm nó vào, và sau đó lại chạy lệnh ở trên.
24 Bạn muốn thêm vài chỉnh sửa vào lần cuối mình đã commit ư? Thế thì cứ sửa chúng đi và sau đó chạy lệnh:
26  $ git commit --amend -a
28 === ... And Then Some ===
30 Let's suppose the previous problem is ten times worse. After a lengthy session you've made a bunch of commits. But you're not quite happy with the way they're organized, and some of those commit messages could use rewording. Then type:
32  $ git rebase -i HEAD~10
34 và and the last 10 commits will appear in your favourite $EDITOR. Một đoạn trích ví dụ:
36     pick 5c6eb73 Added repo.or.cz link
37     pick a311a64 Reordered analogies in "Work How You Want"
38     pick 100834f Added push target to Makefile
40 Then:
42 - Remove commits by deleting lines.
43 - Reorder commits by reordering lines.
44 - Replace `pick` with:
45    * `edit` to mark a commit for amending.
46    * `reword` để thay đổi nhật ký message.
47    * `squash` to merge a commit with the previous one.
48    * `fixup` to merge a commit with the previous one and discard the log message.
50 Ghi lại và thoát ra. Nếu bạn đánh dấu một lần commit dành cho việc sửa đổi, thế thì
51 chạy:
53  $ git commit --amend
55 Cách khác, chạy:
57  $ git rebase --continue
59 So commit early and commit often: you can tidy up later with rebase.
61 === Local Changes Last ===
63 You're working on an active project. You make some local commits over time, and
64 then you sync with the official tree with a merge. This cycle repeats itself a few times before you're ready to push to the central tree.
66 But now the history in your local Git clone is a messy jumble of your changes and the official changes. You'd prefer to see all your changes in one contiguous section, and after all the official changes.
68 Đây chính là công việc dành cho lệnh *git rebase* đã được miêu tả ở trên. Trong nhiều trường hợp bạn có thể sử dụng
69 cờ *--onto* và and avoid interaction.
71 Also see *git help rebase* for detailed examples of this amazing command. You can split commits. You can even rearrange branches of a tree.
73 === Viết Lại Lịch Sử ===
75 Occasionally, bạn muốn việc quản lý mã nguồn giống việc người ta vẽ quivalent of airbrushing people out
76 of official photos, erasing them from history in a Stalinesque fashion. Ví
77 dụ, giả sử chúng ta có ý định phát hành dự án, nhưng lại dibut it involves a file that
78 phải được giữ bí mật vì lý do nào đó. Có lẽ tôi đã quên khi ghi lại số thẻ tín dụng vào trong một tệp tin
79 văn bản và ngẫu nhiên nó được thêm vào trong dự án. Việc xóa tệp tin này là
80 chưa đủ, bởi vì ta có thể đọc nó từ lần commit cũ. Chúng ta phải gỡ bỏ
81 tệp tin này từ tất cả các lần đã commit:
83  $ git filter-branch --tree-filter 'rm top/secret/file' HEAD
85 Xem *git help filter-branch*, nội dung của nó sẽ thảo luận về ví dụ này và đưa ra
86 một cách thức nhanh hơn. Đại thể, lệnh *filter-branch* lets you alter large sections of history
87 chỉ bằng một lệnh đơn.
89 Afterwards, the +.git/refs/original+ directory describes the state of affairs before the operation. Check the filter-branch command did what you wanted, then delete this directory if you wish to run more filter-branch commands.
91 Cuối cùng, replace clones of your project with your revised version if you want to interact with them later.
93 === Making History ===
95 [[makinghistory]]
96 Want to migrate a project to Git? If it's managed with one of the more well-known systems, then chances are someone has already written a script to export the whole history to Git.
98 Otherwise, look up *git fast-import*, which reads text input in a specific
99 format to create Git history from scratch. Typically a script using this
100 command is hastily cobbled together and run once, migrating the project in a
101 single shot.
103 Dưới đây là một ví dụ, dán danh sách theo sau đâu vào một tệp tin tạm thời nào đó, chẳng hạn như là `/tmp/history`:
104 ----------------------------------
105 commit refs/heads/master
106 committer Alice <alice@example.com> Thu, 01 Jan 1970 00:00:00 +0000
107 data <<EOT
108 Initial commit.
111 M 100644 inline hello.c
112 data <<EOT
113 #include <stdio.h>
115 int main() {
116   printf("Hello, world!\n");
117   return 0;
122 commit refs/heads/master
123 committer Bob <bob@example.com> Tue, 14 Mar 2000 01:59:26 -0800
124 data <<EOT
125 Replace printf() with write().
128 M 100644 inline hello.c
129 data <<EOT
130 #include <unistd.h>
132 int main() {
133   write(1, "Hello, world!\n", 14);
134   return 0;
138 ----------------------------------
140 Thế thì tạo một kho Git từ thư mục chứa các tệp tin tạm thời này bằng cách gõ:
142  $ mkdir project; cd project; git init
143  $ git fast-import --date-format=rfc2822 < /tmp/history
145 Bạn có thể checkout phiên bản cuối của dự án với:
147  $ git checkout master .
149 Lệnh *git fast-export* chuyển đổi bất kỳ một kho chứa nào thành định dạng
150 phù hợp với lệnh *git fast-import*, whose output you can study for writing exporters,
151 and also to transport repositories in a human-readable format. Indeed,
152 these commands can send repositories of text files over text-only channels.
154 === Where Did It All Go Wrong? ===
156 You've just discovered a broken feature in your program which you know for sure was working a few months ago. Argh! Where did this bug come from? If only you had been testing the feature as you developed.
158 It's too late for that now. However, provided you've been committing often, Git
159 can pinpoint the problem:
161  $ git bisect start
162  $ git bisect bad HEAD
163  $ git bisect good 1b6d
165 Git checks out a state halfway in between. Test the feature, and if it's still broken:
167  $ git bisect bad
169 If not, replace "bad" with "good". Git again transports you to a state halfway
170 between the known good and bad versions, narrowing down the possibilities.
171 After a few iterations, this binary search will lead you to the commit that
172 caused the trouble. Once you've finished your investigation, return to your
173 original state by typing:
175  $ git bisect reset
177 Instead of testing every change by hand, automate the search by running:
179  $ git bisect run my_script
181 Git uses the return value of the given command, typically a one-off script, to
182 decide whether a change is good or bad: the command should exit with code 0
183 when good, 125 when the change should be skipped, and anything else between 1
184 and 127 if it is bad. A negative return value aborts the bisect.
186 You can do much more: the help page explains how to visualize bisects, examine
187 or replay the bisect log, and eliminate known innocent changes for a speedier
188 search.
190 === Who Made It All Go Wrong? ===
192 Like many other version control systems, Git has a blame command:
194  $ git blame bug.c
196 which annotates every line in the given file showing who last changed it, and when. Unlike many other version control systems, this operation works offline, reading only from local disk.
198 === Kinh Nghiệm Riêng ===
200 Trong một hệ thống quản lý mã nguồn tập trung, thay đổi lịch sử là một việc làm
201 khó khăn, và chỉ thi hành được nếu là người quản trị. Việc nhân bản, tạo nhánh và
202 trộn không thể thiếu việc truyền thông qua mạng. So are basic operations
203 such as browsing history, or committing a change. Trong một số hệ thống khác, người dùng
204 yêu cầu có kết nối mạng chỉ để xem các thay đổi của họ hay mở một tệp tin
205 để biên tập.
207 Hệ thống tập trung không cho phép làm việc mà không có mạng, và đòi hỏi cơ sở hạ tầng mạng máy tính
208 đắt đỏ tốn kém, đặc biệt là khi số nhà phát triển phần mềm tăng lên. Điều quan trọng
209 importantly, all operations are slower to some degree, usually to the point
210 where users shun advanced commands unless absolutely necessary. In extreme
211 cases this is true of even the most basic commands. Khi những người dùng phải chạy các lệnh chạy chậm
212 commands, hiệu suất bị giảm bởi vì sự gián đoạn trong dây truyền làm việc.
214 I experienced these phenomena first-hand. Git was the first version control
215 system I used. I quickly grew accustomed to it, taking many features for
216 granted. I simply assumed other systems were similar: choosing a version
217 control system ought to be no different from choosing a text editor or web
218 browser.
220 I was shocked when later forced to use a centralized system. A flaky internet
221 connection matters little with Git, but makes development unbearable when it
222 needs to be as reliable as local disk. Thêm nữa, I found myself conditioned
223 to avoid certain commands because of the latencies involved, which ultimately
224 prevented me from following my desired work flow.
226 Khi tôi phải chạy những lệnh chậm chạp, việc làm ngắt quãng việc suy nghĩ
227 dealt a disproportionate amount of damage. Trong khi chờ cho việc truyền thông
228 với máy chủ hoàn tất, tôi sẽ phải làm một vài thứ gì đó khác để lấp chỗ trống, chẳng hạn như
229 lấy thư điện tử hay viết tài liệu. Sau một khoảng thời gian tôi quay trở lại nhiệm vụ
230 chính của mình, lệnh đã hoàn tất từ lâu rồi, và tôi phải lãng phí thêm nhiều thời gian nữa
231 để nhớ lại xem mình đang làm gì. Con người thường dở khi phải  Humans are bad at context switching.
233 There was also an interesting tragedy-of-the-commons effect: anticipating
234 network congestion, nhiều cá nhân riêng lẻ có thể chiếm dụng nhiều lưu lượng mạng hơn cần thiết
235 trên các tác vụ khác nhau various operations in an attempt to reduce future delays. The combined efforts
236 intensified congestion, encouraging individuals to consume even more bandwidth
237 next time to avoid even longer delays.