Mercurial > hgbook
annotate en/undo.tex @ 121:9094c9fda8ec
Start chapter on error recovery.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Wed, 15 Nov 2006 15:59:41 -0800 |
parents | |
children | 3af28630fe8c |
rev | line source |
---|---|
121
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 \chapter{Finding and fixing your mistakes} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 \label{chap:undo} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 To err might be human, but to really handle the consequences well |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 takes a top-notch revision control system. In this chapter, we'll |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 discuss some of the techniques you can use when you find that a |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 problem has crept into your project. Mercurial has some highly |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 capable features that will help you to isolate the sources of |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 problems, and to handle them appropriately. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
11 \section{Easily recovered errors} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
12 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 \subsection{The accidental commit} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 I have the occasional but persistent problem of typing rather more |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 quickly than I can think, which sometimes results in me committing a |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 changeset that is either incomplete or plain wrong. In my case, the |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 usual kind of incomplete changeset is one in which I've created a new |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 source file, but forgotten to \hgcmd{add} it. A ``plain wrong'' |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 changeset is not as common, but no less annoying. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
21 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 \subsection{Rolling back a transaction} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
23 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
24 In section~\ref{sec:concepts:txn}, I mentioned that Mercurial treats |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
25 each modification of a repository as a \emph{transaction}. Every time |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 you commit a changeset or pull changes from another repository, |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
27 Mercurial remembers what you did. You can undo, or \emph{roll back}, |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
28 exactly one of these actions using the \hgcmd{rollback} command. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
29 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
30 Here's a mistake that I often find myself making: committing a change |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
31 in which I've created a new file, but forgotten to \hgcmd{add} it. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
32 \interaction{rollback.commit} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
33 Looking at the output of \hgcmd{status} after the commit immediately |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
34 confirms the error. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
35 \interaction{rollback.status} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
36 The commit captured the changes to the file \filename{a}, but not the |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
37 new file \filename{b}. If I were to push this changeset to a |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
38 repository that I shared with a colleague, the chances are high that |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
39 something in \filename{a} would refer to \filename{b}, which would not |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
40 be present in their repository when they pulled my changes. I would |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
41 thus become the object of some indignation. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
42 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
43 However, luck is with me---I've caught my error before I pushed the |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
44 changeset. I use the \hgcmd{rollback} command, and Mercurial makes |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
45 that last changeset vanish. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
46 \interaction{rollback.rollback} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
47 Notice that the changeset is no longer present in the repository's |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
48 history, and the working directory once again thinks that the file |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
49 \filename{a} is modified. The changeset has been completely erased. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
50 I can now safely \hgcmd{add} the file \filename{b}, and rerun my |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
51 commit. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
52 \interaction{rollback.add} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
54 \subsection{The erroneous pull} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
55 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
56 It's common practice with Mercurial to maintain separate development |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
57 branches of a project in different repositories. Your development |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
58 team might have one shared repository for your project's ``0.9'' |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
59 release, and another, containing different changes, for the ``1.0'' |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
60 release. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
61 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
62 Given this, you can imagine that the consequences could be messy if |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
63 you had a local ``0.9'' repository, and accidentally pulled changes |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
64 from the shared ``1.0'' repository into it. At worst, you could be |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
65 paying insufficient attention, and push those changes into the shared |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
66 ``0.9'' tree, confusing your entire team (but don't worry, we'll |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
67 return to this horror scenario later). However, it's more likely that |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
68 you'll notice immediately, because Mercurial will display the URL it's |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
69 pulling from, or you will see it pull a suspiciously large number of |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
70 changes into the repository. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
71 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
72 The \hgcmd{rollback} command will work nicely to expunge all of the |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
73 changesets that you just pulled. Mercurial groups all changes from |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
74 one \hgcmd{pull} into a single transaction, so one \hgcmd{rollback} is |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
75 all you need to undo this mistake. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
76 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
77 \subsection{Rolling back is useless once you've pushed} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
78 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
79 The value of the \hgcmd{rollback} command drops to zero once you've |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
80 pushed your changes to another repository. Rolling back a change |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
81 makes it disappear entirely, but \emph{only} in the repository in |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
82 which you perform the \hgcmd{rollback}. Because a rollback eliminates |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
83 history, there's no way for the disappearance of a change to propagate |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
84 between repositories. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
85 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
86 If you've pushed a change to another repository---particularly if it's |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
87 a shared repository---it has essentially ``escaped into the wild,'' |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
88 and you'll have to recover from your mistake in a different way. What |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
89 will happen if you push a changeset somewhere, then roll it back, then |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
90 pull from the repository you pushed to, is that the changeset will |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
91 reappear in your repository. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
92 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
93 (If you absolutely know for sure that the change you want to roll back |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
94 is the most recent change in the repository that you pushed to, |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
95 \emph{and} you know that nobody else could have pulled it from that |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
96 repository, you can roll back the changeset there, too, but you really |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
97 should really not rely on this working reliably. If you do this, |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
98 sooner or later a change really will make it into a repository that |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
99 you don't directly control (or have forgotten about), and come back to |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
100 bite you.) |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
101 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
102 \subsection{You can only roll back once} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
103 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
104 Mercurial stores exactly one transaction in its transaction log; that |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
105 transaction is the most recent one that occurred in the repository. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
106 This means that you can only roll back one transaction. If you expect |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
107 to be able to roll back one transaction, then its predecessor, this is |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
108 not the behaviour you will get. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
109 \interaction{rollback.twice} |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
110 Once you've rolled back one transaction in a repository, you can't |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
111 roll back again in that repository until you perform another commit or |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
112 pull. |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
113 |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
114 %%% Local Variables: |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
115 %%% mode: latex |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
116 %%% TeX-master: "00book" |
9094c9fda8ec
Start chapter on error recovery.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
117 %%% End: |