de/basic.txt heading fixes.
[gitmagic.git] / en / history.txt
blob94b73e8c347e98d6b27e7594c64a41d29c0a804d
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 Some developers strongly feel history should be immutable, warts and all.
10 Others feel trees should be made presentable before they are unleashed in
11 public. Git accommodates both viewpoints. Like cloning, branching and merging,
12 rewriting history is simply another power Git gives you. It is up to you
13 to use it wisely.
15 === I Stand Corrected ===
17 Did you just commit, but wish you had typed a different message? Then run:
19  $ git commit --amend
21 to change the last message. Realized you forgot to add a file? Run *git add* to
22 add it, and then run the above command.
24 Want to include a few more edits in that last commit? Then make those edits and run:
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 and the last 10 commits will appear in your favourite $EDITOR. A sample excerpt:
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` to change the log 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 Save and quit. If you marked a commit for editing, then
51 run:
53  $ git commit --amend
55 Otherwise, run:
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 This is a job for *git rebase* as described above. In many cases you can use
69 the *--onto* flag 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 === Rewriting History ===
75 Occasionally, you need the source control equivalent of airbrushing people out
76 of official photos, erasing them from history in a Stalinesque fashion. For
77 example, suppose we intend to release a project, but it involves a file that
78 should be kept private for some reason. Perhaps I left my credit card number in
79 a text file and accidentally added it to the project. Deleting the file is
80 insufficient, for the file can be accessed from older commits. We must remove
81 the file from all commits:
83  $ git filter-branch --tree-filter 'rm top/secret/file' HEAD
85 See *git help filter-branch*, which discusses this example and gives a faster
86 method. In general, *filter-branch* lets you alter large sections of history
87 with a single command.
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 Lastly, 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 As an example, paste the following listing into temporary file, such as `/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 Then create a Git repository from this temporary file by typing:
142  $ mkdir project; cd project; git init
143  $ git fast-import --date-format=rfc2822 < /tmp/history
145 You can checkout the latest version of the project with:
147  $ git checkout master .
149 The *git fast-export* command converts any repository to the
150 *git fast-import* format, 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 === Personal Experience ===
200 In a centralized version control system, history modification is a difficult
201 operation, and only available to administrators. Cloning, branching, and
202 merging are impossible without network communication. So are basic operations
203 such as browsing history, or committing a change. In some systems, users
204 require network connectivity just to view their own changes or open a file for
205 editing.
207 Centralized systems preclude working offline, and need more expensive network
208 infrastructure, especially as the number of developers grows. Most
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. When users must run slow
212 commands, productivity suffers because of an interrupted work flow.
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. Additionally, 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 When I had to run a slow command, the interruption to my train of thought
227 dealt a disproportionate amount of damage. While waiting for server
228 communication to complete, I'd do something else to pass the time, such as
229 check email or write documentation. By the time I returned to the original
230 task, the command had finished long ago, and I would waste more time trying to
231 remember what I was doing. Humans are bad at context switching.
233 There was also an interesting tragedy-of-the-commons effect: anticipating
234 network congestion, individuals would consume more bandwidth than necessary on
235 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.