Merge branch 'typos' of git://github.com/sunny256/gitmagic
[gitmagic/gitmagic.git] / en / history.txt
blobbf39734e33dd7fc14dd3196a1f00a11b2b99dfda
1 == Lessons of History ==
3 A consequence of Git's distributed nature is that history can be edited
4 easily. But if you tamper with the past, take care: 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 Of course, if you control all the other trees too, you can simply overwrite
10 them.
12 Some developers strongly feel history should be immutable, warts and all.
13 Others feel trees should be made presentable before they are unleashed in
14 public. Git accommodates both viewpoints. Like cloning, branching and merging,
15 rewriting history is simply another power Git gives you. It is up to you
16 to use it wisely.
18 === I Stand Corrected ===
20 Did you just commit, but wish you had typed a different message? Then run:
22  $ git commit --amend
24 to change the last message. Realized you forgot to add a file? Run *git add* to
25 add it, and then run the above command.
27 Want to include a few more edits in that last commit? Then make those edits and run:
29  $ git commit --amend -a
31 === ... And Then Some ===
33 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:
35  $ git rebase -i HEAD~10
37 and the last 10 commits will appear in your favourite $EDITOR. A sample excerpt:
39     pick 5c6eb73 Added repo.or.cz link
40     pick a311a64 Reordered analogies in "Work How You Want"
41     pick 100834f Added push target to Makefile
43 Then:
45 - Remove commits by deleting lines.
46 - Reorder commits by reordering lines.
47 - Replace "pick" with "edit" to mark a commit for amending.
48 - Replace "pick" with "squash" to merge a commit with the previous one.
50 If you marked a commit for editing, then run:
52  $ git commit --amend
54 Otherwise, run:
56  $ git rebase --continue
58 So commit early and commit often: you can easily tidy up later with rebase.
60 === Local Changes Last ===
62 You're working on an active project. You make some local commits over time, and
63 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.
65 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.
67 This is a job for *git rebase* as described above. In many cases you can use
68 the *--onto* flag and avoid interaction.
70 Also see *git help rebase* for detailed examples of this amazing command. You can split commits. You can even rearrange branches of a tree.
72 === Rewriting History ===
74 Occasionally, you need the source control equivalent of airbrushing people out
75 of official photos, erasing them from history in a Stalinesque fashion. For
76 example, suppose we intend to release a project, but it involves a file that
77 should be kept private for some reason. Perhaps I left my credit card number in
78 a text file and accidentally added it to the project. Deleting the file is
79 insufficient, for the file can be accessed from older commits. We must remove
80 the file from all commits:
82  $ git filter-branch --tree-filter 'rm top/secret/file' HEAD
84 See *git help filter-branch*, which discusses this example and gives a faster
85 method. In general, *filter-branch* lets you alter large sections of history
86 with a single command.
88 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.
90 Lastly, replace clones of your project with your revised version if you want to interact with them later.
92 === Making History ===
94 [[makinghistory]]
95 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.
97 Otherwise, look up *git fast-import*, which reads text input in a specific
98 format to create Git history from scratch. Typically a script using this
99 command is hastily cobbled together and run once, migrating the project in a
100 single shot.
102 As an example, paste the following listing into temporary file, such as `/tmp/history`:
103 ----------------------------------
104 commit refs/heads/master
105 committer Alice <alice@example.com> Thu, 01 Jan 1970 00:00:00 +0000
106 data <<EOT
107 Initial commit.
110 M 100644 inline hello.c
111 data <<EOT
112 #include <stdio.h>
114 int main() {
115   printf("Hello, world!\n");
116   return 0;
121 commit refs/heads/master
122 committer Bob <bob@example.com> Tue, 14 Mar 2000 01:59:26 -0800
123 data <<EOT
124 Replace printf() with write().
127 M 100644 inline hello.c
128 data <<EOT
129 #include <unistd.h>
131 int main() {
132   write(1, "Hello, world!\n", 14);
133   return 0;
137 ----------------------------------
139 Then create a Git repository from this temporary file by typing:
141  $ mkdir project; cd project; git init
142  $ git fast-import --date-format=rfc2822 < /tmp/history
144 You can checkout the latest version of the project with:
146  $ git checkout master .
148 The *git fast-export* command converts any git repository to the
149 *git fast-import* format, whose output you can study for writing exporters,
150 and also to transport git repositories in a human-readable format. Indeed,
151 these commands can send repositories of text files over text-only channels.
153 === Where Did It All Go Wrong? ===
155 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.
157 It's too late for that now. However, provided you've been committing often, Git
158 can pinpoint the problem:
160  $ git bisect start
161  $ git bisect bad HEAD
162  $ git bisect good 1bd6
164 Git checks out a state halfway in between. Test the feature, and if it's still broken:
166  $ git bisect bad
168 If not, replace "bad" with "good". Git again transports you to a state halfway
169 between the known good and bad versions, narrowing down the possibilities.
170 After a few iterations, this binary search will lead you to the commit that
171 caused the trouble. Once you've finished your investigation, return to your
172 original state by typing:
174  $ git bisect reset
176 Instead of testing every change by hand, automate the search by running:
178  $ git bisect run my_script
180 Git uses the return value of the given command, typically a one-off script, to
181 decide whether a change is good or bad: the command should exit with code 0
182 when good, 125 when the change should be skipped, and anything else between 1
183 and 127 if it is bad. A negative return value aborts the bisect.
185 You can do much more: the help page explains how to visualize bisects, examine
186 or replay the bisect log, and eliminate known innocent changes for a speedier
187 search.
189 === Who Made It All Go Wrong? ===
191 Like many other version control systems, Git has a blame command:
193  $ git blame bug.c
195 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.
197 === Personal Experience ===
199 In a centralized version control system, history modification is a difficult
200 operation, and only available to administrators. Cloning, branching, and
201 merging are impossible without network communication. So are basic operations
202 such as browsing history, or committing a change. In some systems, users
203 require network connectivity just to view their own changes or open a file for
204 editing.
206 Centralized systems preclude working offline, and need more expensive network
207 infrastructure, especially as the number of developers grows. Most
208 importantly, all operations are slower to some degree, usually to the point
209 where users shun advanced commands unless absolutely necessary. In extreme
210 cases this is true of even the most basic commands. When users must run slow
211 commands, productivity suffers because of an interrupted work flow.
213 I experienced these phenomena first-hand. Git was the first version control
214 system I used. I quickly grew accustomed to it, taking many features for
215 granted. I simply assumed other systems were similar: choosing a version
216 control system ought to be no different from choosing a text editor or web
217 browser.
219 I was shocked when later forced to use a centralized system. A flaky internet
220 connection matters little with Git, but makes development unbearable when it
221 needs to be as reliable as local disk. Additionally, I found myself conditioned
222 to avoid certain commands because of the latencies involved, which ultimately
223 prevented me from following my desired work flow.
225 When I had to run a slow command, the interruption to my train of thought
226 dealt a disproportionate amount of damage. While waiting for server
227 communication to complete, I'd do something else to pass the time, such as
228 check email or write documentation. By the time I returned to the original
229 task, the command had finished long ago, and I would waste more time trying to
230 remember what I was doing. Humans are bad at context switching.
232 There was also an interesting tragedy-of-the-commons effect: anticipating
233 network congestion, individuals would consume more bandwidth than necessary on
234 various operations in an attempt to reduce future delays. The combined efforts
235 intensified congestion, encouraging individuals to consume even more bandwidth
236 next time to avoid even longer delays.