Svn 简明教程

SVN - Fix Mistakes

假设 Jerry 意外修改了 array.c 文件,并且出现了编译错误。现在他要抛弃更改。在这种情况下,“恢复”操作将有所帮助。“恢复”操作将撤销对文件或目录的任何本地更改,并解决任何冲突的状态。

Suppose Jerry accidently modifies array.c file and he is getting compilation errors. Now he wants to throw away the changes. In this situation, 'revert' operation will help. Revert operation will undo any local changes to a file or directory and resolve any conflicted states.

[jerry@CentOS trunk]$ svn status

上述命令将产生以下结果。

Above command will produce the following result.

M       array.c

让我们尝试按照以下方式创建数组:

Let’s try to make array as follows:

[jerry@CentOS trunk]$ make array

上述命令将产生以下结果。

Above command will produce the following result.

cc     array.c   -o array
array.c: In function ‘main’:
array.c:26: error: ‘n’ undeclared (first use in this function)
array.c:26: error: (Each undeclared identifier is reported only once
array.c:26: error: for each function it appears in.)
array.c:34: error: ‘arr’ undeclared (first use in this function)
make: *** [array] Error 1

Jerry 对 array.c 文件执行“恢复”操作。

Jerry performs 'revert' operation on array.c file.

[jerry@CentOS trunk]$ svn revert array.c
Reverted 'array.c'

[jerry@CentOS trunk]$ svn status
[jerry@CentOS trunk]$

现在编译代码。

Now compile the code.

[jerry@CentOS trunk]$ make array
cc     array.c   -o array

在恢复操作之后,他的工作副本将恢复到其初始状态。“恢复”操作可以恢复单个文件以及完整的目录。要恢复目录,请使用 -R 选项,如下所示。

After the revert operation, his working copy is back to its original state. Revert operation can revert a single file as well as a complete directory. To revert a directory, use -R option as shown below.

[jerry@CentOS project_repo]$ pwd
/home/jerry/project_repo

[jerry@CentOS project_repo]$ svn revert -R trunk

到目前为止,我们已经了解了如何恢复已对工作副本进行的更改。但是,如果你想要恢复已提交的修订版本该怎么办呢!版本控制系统工具不允许从存储库中删除历史记录。我们只能追加历史记录。即使从存储库中删除文件,也会发生这种情况。要撤消旧版本,我们必须还原旧版本中所做的所有更改,然后提交一个新版本。这称为反向合并。

Till now, we have seen how to revert changes, which has been made to the working copy. But what if you want to revert a committed revision! Version Control System tool doesn’t allow to delete history from the repository. We can only append history. It will happen even if you delete files from the repository. To undo an old revision, we have to reverse whatever changes were made in the old revision and then commit a new revision. This is called a reverse merge.

让我们假设 Jerry 添加了线性搜索操作的代码。验证后,他提交了自己的更改。

Let us suppose Jerry adds a code for linear search operation. After verification he commits his changes.

[jerry@CentOS trunk]$ svn diff
Index: array.c
===================================================================
--- array.c   (revision 21)
+++ array.c   (working copy)
@@ -2,6 +2,16 @@

 #define MAX 16

+int linear_search(int *arr, int n, int key)
+{
+   int i;
+
+   for (i = 0; i < n; ++i)
+      if (arr[i] == key)
+         return i;
+   return -1;
+}
+
 void bubble_sort(int *arr, int n)
 {
    int i, j, temp, flag = 1;

[jerry@CentOS trunk]$ svn status
?       array
M       array.c

[jerry@CentOS trunk]$ svn commit -m "Added code for linear search"
Sending        trunk/array.c
Transmitting file data .
Committed revision 22.

Jerry 很想知道 Tom 在做什么。于是他检查了 Subversion 日志消息。

Jerry is curious about what Tom is doing. So he checks the Subversion log messages.

[jerry@CentOS trunk]$ svn log

上述命令将产生以下结果。

The above command will produce the following result.

------------------------------------------------------------------------
r5 | tom   | 2013-08-24 17:15:28 +0530 (Sat, 24 Aug 2013) | 1 line

Add binary search operation
------------------------------------------------------------------------
r4 | jerry | 2013-08-18 20:43:25 +0530 (Sun, 18 Aug 2013) | 1 line

Add function to accept input and to display array contents

查看日志消息后,Jerry 意识到自己犯了一个严重的错误。因为 Tom 已经实现了二进制搜索操作,它比线性搜索更好;他的代码是多余的,现在 Jerry 必须将他的更改还原到以前的修订版本。因此,首先找到存储库的当前修订版本。当前,存储库位于修订版本 22,我们必须将其恢复到以前的修订版本,即修订版本 21。

After viewing the log messages, Jerry realizes that he did a serious mistake. Because Tom already implemented binary search operation, which is better than the linear search; his code is redundant, and now Jerry has to revert his changes to the previous revision. So, first find the current revision of the repository. Currently, the repository is at revision 22 and we have to revert it to the previous revision, i.e. revision 21.

[jerry@CentOS trunk]$ svn up
At revision 22.

[jerry@CentOS trunk]$ svn merge -r 22:21 array.c
--- Reverse-merging r22 into 'array.c':
U    array.c

[jerry@CentOS trunk]$ svn commit -m "Reverted to revision 21"
Sending        trunk/array.c
Transmitting file data .
Committed revision 23.