# 常用 SVN 命令简介及使用

#### &#x20;博客作者：联系请[点击](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)，搬运不易，希望请作者喝咖啡，可以点击联系[博客作者](https://hezhiqiang.gitbook.io/about-the-author/lian-xi-zuo-zhe)

## 前言

&#x20;         [SVN](http://subversion.apache.org/)（Subversion）是一个自由、开源的项目源代码版本控制工具。目前，绝大多数开源软件和企业代码管理，都使用SVN作为代码版本管理软件。

Subversion将文件存放在中心版本库里，这个版本库很像一个普通的文件服务器。不同的是，它可以记录每一次文件和目录的修改情况，这样就可以在需要回滚时，将数据恢复到以前的版本，并可以查看数据的更改细节。

### **SVN 官方网址：**

{% embed url="<http://subversion.apache.org/>" %}

## 一、SVN 服务器安装及配置：

### 1. 添加SVN高版本YUM源

```bash
vim /etc/yum.repos.d/wandisco-svn.repo
```

#### CentOS/RHEL 7：

```bash
[WandiscoSVN]
name=Wandisco SVN Repo
baseurl=http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/$basearch/
enabled=1
gpgcheck=0
```

#### CentOS/RHEL 6：

```bash
[WandiscoSVN]
name=Wandisco SVN Repo
baseurl=http://opensource.wandisco.com/centos/6/svn-1.9/RPMS/$basearch/
enabled=1
gpgcheck=0
```

#### CentOS/RHEL 5：

```bash
[WandiscoSVN]
name=Wandisco SVN Repo
baseurl=http://opensource.wandisco.com/centos/5/svn-1.9/RPMS/$basearch/
enabled=1
gpgcheck=0
```

### 2. 安装第三方Yum源及清除Yum源缓存

```bash
yum install -y epel-release yum-utils 
yum clean all
yum makecache
```

### 3. 安装最新版Subversion

```bash
yum install -y subversion
```

### 4. 查看安装的Subversion版本

```bash
[root@svn ~]# svn --version
svn，版本 1.9.12 (r1863368)
   编译于 Aug 14 2019，16:35:53 在 x86_64-redhat-linux-gnu

Copyright (C) 2019 The Apache Software Foundation.
This software consists of contributions made by many people;
see the NOTICE file for more information.
Subversion is open source software, see http://subversion.apache.org/

可使用以下的版本库访问模块:

* ra_svn : 使用 svn 网络协议访问版本库的模块。  - 使用 Cyrus SASL 认证
  - 处理“svn”方案
* ra_local : 访问本地磁盘的版本库模块。
  - 处理“file”方案
* ra_serf : Module for accessing a repository via WebDAV protocol using serf.
  - using serf 1.3.9 (compiled with 1.3.9)
  - 处理“http”方案
  - 处理“https”方案

The following authentication credential caches are available:

* Plaintext cache in /root/.subversion
* Gnome Keyring
* GPG-Agent
```

{% hint style="danger" %}
**注意：如果是CentOS/RHEL 8则需要手动下载最新版相关rpm包**
{% endhint %}

#### 访问下面地址 ：

<http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/>

#### 下载如下安装包：

```bash
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/libserf-1.3.9-1.el7.x86_64.rpm
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/libserf-debuginfo-1.3.9-1.el7.x86_64.rpm
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/libserf-devel-1.3.9-1.el7.x86_64.rpm
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/mod_dav_svn-1.9.9-1.x86_64.rpm
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/subversion-1.9.9-1.x86_64.rpm
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/subversion-devel-1.9.9-1.x86_64.rpm
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/subversion-gnome-1.9.9-1.x86_64.rpm
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/subversion-perl-1.9.9-1.x86_64.rpm
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/subversion-python-1.9.9-1.x86_64.rpm
wget http://opensource.wandisco.com/centos/7/svn-1.9/RPMS/x86_64/subversion-tools-1.9.9-1.x86_64.rpm
```

#### CentOS/RHEL 8安装

{% hint style="danger" %}
**注意：需要在当前下载rpm包的目录下执行**
{% endhint %}

```bash
yum -y install --skip-broken *.rpm
rm -rf *.rpm                   #安装完需要把下载的rpm包删除
```

#### CentOS/RHEL 8查看安装版本是否安装成功的

```bash
[root@SVN ~]# svn --version
svn，版本 1.9.9 (r1835931)
   编译于 Jul 25 2018，12:41:27 在 x86_64-redhat-linux-gnu

Copyright (C) 2018 The Apache Software Foundation.
This software consists of contributions made by many people;
see the NOTICE file for more information.
Subversion is open source software, see http://subversion.apache.org/

可使用以下的版本库访问模块:

* ra_svn : 使用 svn 网络协议访问版本库的模块。  - 使用 Cyrus SASL 认证
  - 处理“svn”方案
* ra_local : 访问本地磁盘的版本库模块。
  - 处理“file”方案
* ra_serf : Module for accessing a repository via WebDAV protocol using serf.
  - using serf 1.3.9 (compiled with 1.3.8)
  - 处理“http”方案
  - 处理“https”方案

The following authentication credential caches are available:

* Plaintext cache in /root/.subversion
* Gnome Keyring
* GPG-Agent

[root@SVN ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)
[root@SVN ~]# uname -r
4.18.0-80.el8.x86_64
```

## 二、**创建svn资源库目录及配置**

### &#x20;1. **创建svn资源库目录及初始化项目**

```bash
mkdir -p /var/svn/project
svnadmin create /var/svn/project
```

{% hint style="info" %}
初始化项目完成后会看到如下文件
{% endhint %}

```bash
[root@svn ~]# ll /var/svn/project
总用量 8
drwxr-xr-x. 2 root root  76 1月   4 19:47 conf
drwxr-sr-x. 6 root root 233 1月   4 19:47 db
-r--r--r--. 1 root root   2 1月   4 19:47 format
drwxr-xr-x. 2 root root 231 1月   4 19:47 hooks
drwxr-xr-x. 2 root root  41 1月   4 19:47 locks
-rw-r--r--. 1 root root 246 1月   4 19:47 README.txt
```

### &#x20;2. **配置**

{% hint style="info" %}
进入conf文件夹，这个是存放配置文件的
{% endhint %}

```bash
[root@svn conf]# pwd
/var/svn/project/conf
[root@svn conf]# ls
authz  hooks-env.tmpl  passwd  svnserve.conf
```

{% hint style="warning" %}
&#x20;**其中**：

#### hooks-env.tmpl         #该文件是示例钩子脚本环境配置文件 authz                           #该文件是权限控制文件 passwd                       #该文件是帐号密码文件 svnserve.conf           #该文件是SVN服务配置文件

{% endhint %}

{% hint style="info" %}

#### 配置 svnserv.conf

{% endhint %}

{% hint style="danger" %}

#### 注：采用默认配置，以下语句都必须顶格写，左侧不能留空格，否则会出错。

{% endhint %}

```bash
vim svnserv.conf
[general]
anon-access = none       # 使非授权用户无法访问
auth-access = write      # 使授权用户有写权限
password-db = passwd     # 用户密码文件
authz-db = authz         # 访问控制文件
realm = /var/svn/project # 认证命名空间，subversion会在认证提示里显示，并且作为凭证缓存的关键字。
```

{% hint style="info" %}

#### 配置passwd   （格式：帐号=密码)

{% endhint %}

```
[root@svn conf]# vim passwd
test1 = 123456
test2 = 123456
```

{% hint style="info" %}

#### 配置authz      (格式：帐号 = 权限)

{% endhint %}

```bash
[/]
username = rw 
# r：读，w：写
注：还有更加细致的权限配置，在这里就不在写了，大家可以查看SVN文档。
```

### 3. 启动与停止SVN服务

**启动**

```bash
svnserve -d -r [dir]
例如：svnserve -d -r /var/svn
```

**停止**

```bash
killall svnserve
```

{% hint style="danger" %}

#### 注：默认的端口是3690，在检出提交操作之前请检测此端口是否允许出站入站。如不允许，请设置好防火墙或安全组。

{% endhint %}

### 4. 客户端访问

{% hint style="warning" %}
**客户端访问格式：svn://IP/project**
{% endhint %}

比如:我创建的项目目录是project，访问就是如下图。

![](/files/-Lxk_v0OYnr5noqoxYHu)

{% hint style="danger" %}

#### 注意：如果授权的账号只有子目录权限则需要输入主项目下的子目录路径才可以认证成功

{% endhint %}

### 5. Windows客户端下载地址

{% embed url="<https://tortoisesvn.net/downloads.zh.html>" %}

{% hint style="danger" %}

#### 需要根据安装的Windows系统版本下载相对应软件安装包和语言汉化包。

{% endhint %}

![](/files/-LxkhDZLKUZjCJTCdYbQ)

{% hint style="danger" %}

#### 注意：下载中文汉化包需要与下载的SVN客户端相对应版本，安装好汉化包需要重启电脑才能生效。

{% endhint %}

## 三、 SVN常用命令

### 1. 将文件checkout到本地目录

```bash
svn checkout path           #path是服务器上的目录
例如：svn checkout svn://192.168.1.1/pro/domain
简写：svn co
```

### 2、往版本库中添加新的文件

```bash
svn add file
例如：svn add test.php     #添加test.php
     svn add *.php        #添加当前目录下所有的php文件
```

### 3、将改动的文件提交到版本库

```bash
svn commit -m "LogMessage" [-N] [--no-unlock] PATH  #如果选择了保持锁，就使用--no-unlock开关
例如：svn commit -m "add test file for my test" test.php
简写：svn ci
```

### 4、加锁/解锁

```bash
svn lock -m "LockMessage" [--force] PATH
例如：svn lock -m "lock test file" test.php
     svn unlock PATH
```

### 5、更新到某个版本

```bash
svn update -r m path
例如：
svn update                   #如果后面没有目录，默认将当前目录以及子目录下的所有文件都更新到最新版本。
svn update -r 200 test.php   #将版本库中的文件test.php还原到版本200
svn update test.php          #更新，于版本库同步。如果在提交的时候提示过期的话，是因为冲突，需要先update，修改文件，然后清除svn resolved，最后再提交commit
简写：svn up
```

### 6、查看文件或者目录状态

```bash
1）svn status path       #目录下的文件和子目录的状态，正常状态不显示
 ?：不在svn的控制中；M：内容被修改；C：发生冲突；A：预定加入到版本库；K：被锁定
  
2）svn status -v path    #显示文件和子目录状态
  第一列保持相同，第二列显示工作版本号，第三和第四列显示最后一次修改的版本号和修改人。
  
  注：svn status、svn diff和 svn revert这三条命令在没有网络的情况下也可以执行的，原因是svn在本地的.svn中保留了本地版本的原始拷贝。
简写：svn st
```

### 7、删除文件

```bash
svn delete path -m "delete test fle"
例如：svn delete svn://192.168.1.1/pro/domain/test.php -m "delete test file"
或者直接svn delete test.php 然后再svn ci -m 'delete test file'，推荐使用这种
简写：svn (del, remove, rm)
```

### 8、查看日志

```bash
svn log path
例如：svn log test.php   #显示这个文件的所有修改记录，及其版本号的变化
```

### 9、查看文件详细信息

```bash
svn info path
例如：svn info test.php
```

### 10、比较差异

```bash
svn diff path          #将修改的文件与基础版本比较
例如：svn diff test.php
svn diff -r m:n path   #对版本m和版本n比较差异
例如：svn diff -r 200:201 test.php
简写：svn di
```

### 11、将两个版本之间的差异合并到当前文件

```bash
svn merge -r m:n path
例如：svn merge -r 200:205 test.php  #将版本200与205之间的差异合并到当前文件，但是一般都会产生冲突，需要处理一下
```

### 12、SVN 帮助

```
svn help
svn help ci
```

{% hint style="info" %}

#### 以上是常用命令，下面写几个不经常用的

{% endhint %}

### 13、版本库下的文件和目录列表

```bash
svn list path              #显示path目录下的所有属于版本库的文件和目录
简写：svn ls
```

### 14、创建纳入版本控制下的新目录

```bash
svn mkdir:                 #创建纳入版本控制下的新目录。
用法:     
1、mkdir PATH...
2、mkdir URL...
创建版本控制的目录。
1、每一个以工作副本 PATH 指定的目录，都会创建在本地端，并且加入新增调度，以待下一次的提交。
2、每个以URL指定的目录，都会透过立即提交于仓库中创建。
在这两个情况下，所有的中间目录都必须事先存在。
```

### 15、恢复本地修改

```bash
svn revert:                 #恢复原始未改变的工作副本文件 (恢复大部份的本地修改)。
revert:
用法: revert PATH...
注意: 本子命令不会存取网络，并且会解除冲突的状况。但是它不会恢复被删除的目录
```

### 16、代码库URL变更。

```bash
svn switch (sw):            #更新工作副本至不同的URL。
用法: 
1、switch URL [PATH]
2、switch --relocate FROM TO [PATH...]
```

{% hint style="info" %}
1、更新你的工作副本，映射到一个新的URL，其行为跟“svn update”很像，也会将 服务器上文件与本地文件合并。这是将工作副本对应到同一仓库中某个分支或者标记的 方法。&#x20;

2、改写工作副本的URL元数据，以反映单纯的URL上的改变。当仓库的根URL变动 (比如方案名或是主机名称变动)，但是工作副本仍旧对映到同一仓库的同一目录时使用 这个命令更新工作副本与仓库的对应关系。
{% endhint %}

### 17、解决冲突

```bash
svn resolved:              #移除工作副本的目录或文件的“冲突”状态。
用法: resolved PATH...
注意: 本子命令不会依语法来解决冲突或是移除冲突标记；它只是移除冲突的相关文件，然后让 PATH 可以再次提交。
```

### 18、输出指定文件或URL的内容。

```bash
svn cat 目标[@版本]...     #如果指定了版本，将从指定的版本开始查找。
svn cat -r PREV filename > filename (PREV 是上一版本,也可以写具体版本号,这样输出结果是可以提交的)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/about-the-author/yun-wei-gong-ju-shi-yong/chang-yong-svn-ming-ling-jian-jie-ji-shi-yong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
