Merge branch 'master' of https://github.com/matrig/gitmagic
[gitmagic.git] / en / history.txt
blobdfe9d6914943898f7e9bc8d8022cd2e0953959ed
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 Suppose the previous problem is ten times worse. After a lengthy session you've
31 made a bunch of commits. But you're not quite happy with the way they're
32 organized, and some of those commit messages could use rewording. Then type:
34  $ git rebase -i HEAD~10
36 and the last 10 commits will appear in your favourite $EDITOR. A sample excerpt:
38     pick 5c6eb73 Added repo.or.cz link
39     pick a311a64 Reordered analogies in "Work How You Want"
40     pick 100834f Added push target to Makefile
42 Older commits precede newer commits in this list, unlike the `log` command.
43 Here, 5c6eb73 is the oldest commit, and 100834f is the newest. Then:
45 - Remove commits by deleting lines. Like the revert command, but off the
46   record: it will be as if the commit never existed.
47 - Reorder commits by reordering lines.
48 - Replace `pick` with:
49    * `edit` to mark a commit for amending.
50    * `reword` to change the log message.
51    * `squash` to merge a commit with the previous one.
52    * `fixup` to merge a commit with the previous one and discard the log message.
54 For example, we might replace the second `pick` with `squash`:
56     pick 5c6eb73 Added repo.or.cz link
57     squash a311a64 Reordered analogies in "Work How You Want"
58     pick 100834f Added push target to Makefile
60 After we save and quit, Git merges a311a64 into 5c6eb73. Thus *squash* merges
61 into the next commit up: think ``squash up''.
63 Git then combines their log messages and presents them for editing. The
64 command *fixup* skips this step; the squashed log message is simply discarded.
66 If you marked a commit with *edit*, Git returns you to the past, to the oldest
67 such commit. You can amend the old commit as described in the previous section,
68 and even create new commits that belong here. Once you're pleased with the
69 ``retcon'', go forward in time by running:
71  $ git rebase --continue
73 Git replays commits until the next *edit*, or to the present if none remain.
75 You can also abandon the rebase with:
77  $ git rebase --abort
79 So commit early and commit often: you can tidy up later with rebase.
81 === Local Changes Last ===
83 You're working on an active project. You make some local commits over time, and
84 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.
86 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.
88 This is a job for *git rebase* as described above. In many cases you can use
89 the *--onto* flag and avoid interaction.
91 Also see *git help rebase* for detailed examples of this amazing command. You can split commits. You can even rearrange branches of a tree.
93 Take care: rebase is a powerful command. For complicated rebases, first make a
94 backup with *git clone*.
96 === Rewriting History ===
98 Occasionally, you need the source control equivalent of airbrushing people out
99 of official photos, erasing them from history in a Stalinesque fashion. For
100 example, suppose we intend to release a project, but it involves a file that
101 should be kept private for some reason. Perhaps I left my credit card number in
102 a text file and accidentally added it to the project. Deleting the file is
103 insufficient, for the file can be accessed from older commits. We must remove
104 the file from all commits:
106  $ git filter-branch --tree-filter 'rm top/secret/file' HEAD
108 See *git help filter-branch*, which discusses this example and gives a faster
109 method. In general, *filter-branch* lets you alter large sections of history
110 with a single command.
112 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.
114 Lastly, replace clones of your project with your revised version if you want to interact with them later.
116 === Making History ===
118 [[makinghistory]]
119 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.
121 Otherwise, look up *git fast-import*, which reads text input in a specific
122 format to create Git history from scratch. Typically a script using this
123 command is hastily cobbled together and run once, migrating the project in a
124 single shot.
126 As an example, paste the following listing into temporary file, such as `/tmp/history`:
127 ----------------------------------
128 commit refs/heads/master
129 committer Alice <alice@example.com> Thu, 01 Jan 1970 00:00:00 +0000
130 data <<EOT
131 Initial commit.
134 M 100644 inline hello.c
135 data <<EOT
136 #include <stdio.h>
138 int main() {
139   printf("Hello, world!\n");
140   return 0;
145 commit refs/heads/master
146 committer Bob <bob@example.com> Tue, 14 Mar 2000 01:59:26 -0800
147 data <<EOT
148 Replace printf() with write().
151 M 100644 inline hello.c
152 data <<EOT
153 #include <unistd.h>
155 int main() {
156   write(1, "Hello, world!\n", 14);
157   return 0;
161 ----------------------------------
163 Then create a Git repository from this temporary file by typing:
165  $ mkdir project; cd project; git init
166  $ git fast-import --date-format=rfc2822 < /tmp/history
168 You can checkout the latest version of the project with:
170  $ git checkout master .
172 The *git fast-export* command converts any repository to the
173 *git fast-import* format, whose output you can study for writing exporters,
174 and also to transport repositories in a human-readable format. Indeed,
175 these commands can send repositories of text files over text-only channels.
177 === Where Did It All Go Wrong? ===
179 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.
181 It's too late for that now. However, provided you've been committing often, Git
182 can pinpoint the problem:
184  $ git bisect start
185  $ git bisect bad HEAD
186  $ git bisect good 1b6d
188 Git checks out a state halfway in between. Test the feature, and if it's still broken:
190  $ git bisect bad
192 If not, replace "bad" with "good". Git again transports you to a state halfway
193 between the known good and bad versions, narrowing down the possibilities.
194 After a few iterations, this binary search will lead you to the commit that
195 caused the trouble. Once you've finished your investigation, return to your
196 original state by typing:
198  $ git bisect reset
200 Instead of testing every change by hand, automate the search by running:
202  $ git bisect run my_script
204 Git uses the return value of the given command, typically a one-off script, to
205 decide whether a change is good or bad: the command should exit with code 0
206 when good, 125 when the change should be skipped, and anything else between 1
207 and 127 if it is bad. A negative return value aborts the bisect.
209 You can do much more: the help page explains how to visualize bisects, examine
210 or replay the bisect log, and eliminate known innocent changes for a speedier
211 search.
213 === Who Made It All Go Wrong? ===
215 Like many other version control systems, Git has a blame command:
217  $ git blame bug.c
219 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.
221 === Personal Experience ===
223 In a centralized version control system, history modification is a difficult
224 operation, and only available to administrators. Cloning, branching, and
225 merging are impossible without network communication. So are basic operations
226 such as browsing history, or committing a change. In some systems, users
227 require network connectivity just to view their own changes or open a file for
228 editing.
230 Centralized systems preclude working offline, and need more expensive network
231 infrastructure, especially as the number of developers grows. Most
232 importantly, all operations are slower to some degree, usually to the point
233 where users shun advanced commands unless absolutely necessary. In extreme
234 cases this is true of even the most basic commands. When users must run slow
235 commands, productivity suffers because of an interrupted work flow.
237 I experienced these phenomena first-hand. Git was the first version control
238 system I used. I quickly grew accustomed to it, taking many features for
239 granted. I simply assumed other systems were similar: choosing a version
240 control system ought to be no different from choosing a text editor or web
241 browser.
243 I was shocked when later forced to use a centralized system. A flaky internet
244 connection matters little with Git, but makes development unbearable when it
245 needs to be as reliable as local disk. Additionally, I found myself conditioned
246 to avoid certain commands because of the latencies involved, which ultimately
247 prevented me from following my desired work flow.
249 When I had to run a slow command, the interruption to my train of thought
250 dealt a disproportionate amount of damage. While waiting for server
251 communication to complete, I'd do something else to pass the time, such as
252 check email or write documentation. By the time I returned to the original
253 task, the command had finished long ago, and I would waste more time trying to
254 remember what I was doing. Humans are bad at context switching.
256 There was also an interesting tragedy-of-the-commons effect: anticipating
257 network congestion, individuals would consume more bandwidth than necessary on
258 various operations in an attempt to reduce future delays. The combined efforts
259 intensified congestion, encouraging individuals to consume even more bandwidth
260 next time to avoid even longer delays.