一、SVN啟動
[root@localhost ~]# mkdir /data/svn
[root@localhost ~]# svnadmin create /data/svn/test
[root@localhost ~]# svnserve -d -r /data/svn/test --listen-port 3700
[root@localhost ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.1.69:59878 0.0.0.0:* LISTEN 1676/sshd
tcp 0 0 0.0.0.0:3700 0.0.0.0:* LISTEN 29913/svnserve
-r: 配置方式決定了版本庫訪問方式。
--listen-port: 指定SVN監聽端口,不加此參數,SVN默認監聽3690
由於-r 配置方式的不一樣,SVN啟動就可以有兩種不同的訪問方式
方式一:-r直接指定到版本庫(稱之為單庫svnserve方式)
svnserve -d -r /data/svn/test
在這種情況下,一個svnserve只能為一個版本庫工作。
authz配置文件中對版本庫權限的配置應這樣寫:
[groups]
admin=user1
dev=user2
[/]
@admin=rw
user2=r
* = 其他用戶沒有權限
使用類似這樣的URL:svn://192.168.0.1/ 即可訪問test版本庫。
方式二:指定到版本庫的上級目錄(稱之為多庫svnserve方式)
svnserve -d -r /data/svn
這種情況,一個svnserve可以為多個版本庫工作
authz配置文件中對版本庫權限的配置應這樣寫:
[groups]
admin=user1
dev=user2
[test:/]
@admin=rw
user2=r
[test01:/]
@admin=rw
user2=r
如果此時你還用[/],則表示所有庫的根目錄,同理,[/src]表示所有庫的根目錄下的src目錄。
使用類似這樣的URL:svn://192.168.0.1/test即可訪問test版本庫。
二、創建版本庫
[root@localhost ~]# svnadmin create /data/svn/test
[root@localhost ~]# ll /data/svn/test
total 24
drwxr-xr-x 2 root root 4096 Feb 26 11:10 conf
drwxr-sr-x 6 root root 4096 Feb 26 11:10 db
-r--r--r-- 1 root root 2 Feb 26 11:10 format
drwxr-xr-x 2 root root 4096 Feb 26 11:10 hooks
drwxr-xr-x 2 root root 4096 Feb 26 11:10 locks
-rw-r--r-- 1 root root 229 Feb 26 11:10 README.txt
svn目錄說明:
- dav目錄:是提供apache與mod_dav_svn使用的目錄,讓他們存儲內部數據
- db目錄:就是所有版本控制的數據存放文件
- hooks目錄:放置hook腳本文件的目錄
- locks目錄:用來放置subversion建庫鎖定數據的目錄,用來追蹤存取文件庫的客戶端
- format文件:是一個文本文件,里面只放了一個整數。表示當前文件庫配置的版本號
- conf目錄:是這個倉庫的配置文件(倉庫的用戶訪問賬號、權限等)
進入/opt/svn/runoob01/conf目錄 修改默認配置文件配置,包括svnserve.conf、passwd、authz 配置相關用戶和權限。
1、svn服務配置文件svnserve.conf
svn服務配置文件為版本庫目錄中的文件conf/svnserve.conf。
[root@localhost test]# cd conf/
[root@localhost conf]# ls
authz passwd svnserve.conf
[general]
# anon-access = read 定義非授權用戶的訪問權限,有三種方式: none 、read 、write ,設置為none限制訪問,read為只讀, write為具有讀寫權限,默認為read。
# auth-access = write 定義授權用戶的訪問權限,有三種方式: none 、read 、write ,設置為none限制訪問, read為只讀, write為具有讀寫權限,默認為write 。
# password-db = passwd 定義保存用戶名和密碼的文件名稱,這里為passwd,和該文件位於同一目錄。
# authz-db = authz 定義保存授權信息的文件名稱,這里為authz,和該文件位於同一目錄。
# realm = My First Repository 定義客戶端連接時的“認證命名空間”, Subversion會在認證提示里顯示,並且作為憑證緩存的關鍵字。
[sasl] 用於標識是否進行SASL加密處理
# use-sasl = true 是否開啟sasl
# min-encryption = 0
# max-encryption = 256
變量min-encryption和max-encryption控制服務器所需要的加密強度。
2、用戶名口令文件passwd
用戶名口令文件由svnserve.conf的配置項password-db指定,缺省為conf目錄中的passwd。該文件僅由一個[users]配置段組成。
[users]配置段的配置行格式如下:
[users]
# harry = harryssecret
# sally = sallyssecret
3、權限配置文件
權限配置文件由svnserve.conf的配置項authz-db指定,缺省為conf目錄中的authz。該配置文件由一個[groups]配置段和若干個版本庫路徑權限段組成。
[groups]配置段中配置行格式如下:
[groups]
g_admin = admin,thinker
[admintools:/]
@g_admin = rw
* =
[test:/home/thinker]
thinker = rw
* = r
三、SVN檢出操作
[root@localhost ~]# svn checkout svn://192.168.1.69:3700/test --username=user01
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'user01':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
Checked out revision 0.
[root@localhost ~]# ls 檢出成功后在當前目錄生成test文件夾
anaconda-ks.cfg install.log install.log.syslog test
[root@localhost ~]# ll test/
total 0
查看版本庫信息
[root@localhost ~]# svn info svn://192.168.1.69:3700/test --username=user01
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'user01':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
Path: test
URL: svn://192.168.1.69:3700/test
Repository Root: svn://192.168.1.69:3700/test
Repository UUID: d120c548-f91f-4039-8278-1648c0339d64
Revision: 0
Node Kind: directory
Last Changed Rev: 0
Last Changed Date: 2018-02-26 11:10:56 +0800 (Mon, 26 Feb 2018)
四、SVN提交操作
[root@localhost ~]# cd test/
[root@localhost test]# ls
[root@localhost test]# vim hello.html
[root@localhost test]# svn status
? hello.html
此時hello.html的狀態為?,說明它還未加到版本控制中。
將文件readme加到版本控制,等待提交到版本庫。
[root@localhost test]# svn add hello.html
A hello.html
[root@localhost test]# svn status
A hello.html
此時hello.html的狀態為A,它意味着這個文件已經被成功地添加到了版本控制中。
為了把hello.html存儲到版本庫中,使用commit -m加上注釋信息來提交。
如果你忽略了 -m 選項, SVN會打開一個可以輸入多行的文本編輯器來讓你輸入提交信息。
[root@localhost test]# svn commit -m "Hello"
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'user01':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
Adding hello.html
Transmitting file data .
Committed revision 1.
現在hello.html被成功地添加到了版本庫中,並且修訂版本號自動增加了1。
五、SVN解決沖突
版本沖突原因:
假設 A、B 兩個用戶都在版本號為 100 的時候,更新了 kingtuns.txt 這個文件,A 用戶在修改完成之后提交 kingtuns.txt 到服務器, 這個時候提交成功,這個時候 kingtuns.txt 文件的版本號已經變成 101 了。同時B用戶在版本號為 100 的 kingtuns.txt 文件上作修改, 修改完成之后提交到服務器時,由於不是在當前最新的 101 版本上作的修改,所以導致提交失敗。
使用admin用戶修改hello.html
[root@localhost test]# cat hello.html
HelloWorld! http://www.test.com/tong
[root@localhost test]# cat hello.html
HelloWorld! http://www.test.com/tong
[root@localhost test]# svn diff
Index: hello.html
===================================================================
--- hello.html (revision 1)
+++ hello.html (working copy)
@@ -1 +1 @@
-HelloWorld! http://www.test.com/
+HelloWorld! http://www.test.com/tong
提交修改
[root@localhost test]# svn commit -m "Tong"
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
Sending hello.html
Transmitting file data .
Committed revision 2.
使用user01繼續修改hello.html
[root@localhost test]# svn diff
Index: hello.html
===================================================================
--- hello.html (revision 1)
+++ hello.html (working copy)
@@ -1 +1 @@
-HelloWorld! http://www.test.com/
+HelloWorld! http://www.test.com/test
[root@localhost test]# cat hello.html
HelloWorld! http://www.test.com/test
嘗試提交
[root@localhost test]# svn commit -m "Hello02"
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
Sending hello.html
Transmitting file data .svn: Commit failed (details follow):
svn: File '/hello.html' is out of date
這時我們發現提交失敗了。
因為此時,Hello.html 已經被admin修改並提交到了倉庫。Subversion不會允許user01提交更改,因為admin已經修改了倉庫,所以我們的工作副本已經失效。
為了避免兩人的代碼被互相覆蓋,Subversion不允許我們進行這樣的操作。所以我們在提交更改之前必須先更新工作副本。所以使用update命令,如下:
[root@localhost test]# svn update
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
Conflict discovered in 'hello.html'.
Select: (p) postpone, (df) diff-full, (e) edit,
(mc) mine-conflict, (tc) theirs-conflict,
(s) show all options: mc
G hello.html
Updated to revision 2.
這里輸入"mc",以本地的文件為主。你也可以使用其選項對沖突的文件進行不同的操作。
默認是更新到最新的版本,我們也可以指定更新到哪個版本。
svn update -r6
此時工作副本是和倉庫已經同步,可以安全地提交更改了
[root@localhost test]# svn commit -m "change Hello02"
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
Sending hello.html
Transmitting file data .
Committed revision 3.
六、SVN版本回退
[root@localhost test]# cat hello.html
HelloWorld! http://www.test.com/test?
[root@localhost test]# svn status
M hello.html
[root@localhost test]# svn revert hello.html
Reverted 'hello.html'
[root@localhost test]# svn status
[root@localhost test]# cat hello.html
HelloWorld! http://www.test.com/test
revert 操作不單單可以使單個文件恢復原狀, 而且可以使整個目錄恢復原狀。恢復目錄用 -R 命令,如下:
svn revert -R test
但是,假如我們想恢復一個已經提交的版本怎么辦。
為了消除一個舊版本,我們必須撤銷舊版本里的所有更改然后提交一個新版本。這種操作叫做 reverse merge。
首先,找到倉庫的當前版本,現在是版本22,我們要撤銷回之前的版本,比如版本21:
svn merge -r 22:21 hello.html
七、SVN查看歷史信息
通過svn命令可以根據時間或修訂號去除過去的版本,或者某一版本所做的具體的修改。以下四個命令可以用來查看svn 的歷史:
- svn log: 用來展示svn 的版本作者、日期、路徑等等。
- svn diff: 用來顯示特定修改的行級詳細信息。
- svn cat: 取得在特定版本的某文件顯示在當前屏幕。
- svn list: 顯示一個目錄或某一版本存在的文件。
(1)svn log
可以顯示所有的信息,如果只希望查看特定的某兩個版本之間的信息,可以使用:
查看當前版本號
[root@localhost test]# svn info
Path: .
URL: svn://192.168.1.69:3700/test
Repository Root: svn://192.168.1.69:3700/test
Repository UUID: d120c548-f91f-4039-8278-1648c0339d64
Revision: 2
Node Kind: directory
Schedule: normal
Last Changed Author: admin
Last Changed Rev: 2
Last Changed Date: 2018-02-26 12:08:37 +0800 (Mon, 26 Feb 2018)
[root@localhost test]# svn log -r 1:2
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
------------------------------------------------------------------------
r1 | user01 | 2018-02-26 12:01:34 +0800 (Mon, 26 Feb 2018) | 1 line
Hello
------------------------------------------------------------------------
r2 | admin | 2018-02-26 12:08:37 +0800 (Mon, 26 Feb 2018) | 1 line
Tong
------------------------------------------------------------------------
如果只想查看某一個文件的版本修改信息,可以使用 svn log 文件路徑。
[root@localhost test]# svn log hello.html
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
------------------------------------------------------------------------
r3 | admin | 2018-02-26 12:13:32 +0800 (Mon, 26 Feb 2018) | 1 line
change Hello02
------------------------------------------------------------------------
r2 | admin | 2018-02-26 12:08:37 +0800 (Mon, 26 Feb 2018) | 1 line
Tong
------------------------------------------------------------------------
r1 | user01 | 2018-02-26 12:01:34 +0800 (Mon, 26 Feb 2018) | 1 line
Hello
------------------------------------------------------------------------
如果希望得到目錄的信息要加 -v。
如果希望顯示限定N條記錄的目錄信息,使用 svn log -l N -v。
[root@localhost test]# svn log -l 2 -v
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
------------------------------------------------------------------------
r2 | admin | 2018-02-26 12:08:37 +0800 (Mon, 26 Feb 2018) | 1 line
Changed paths:
M /hello.html
Tong
------------------------------------------------------------------------
r1 | user01 | 2018-02-26 12:01:34 +0800 (Mon, 26 Feb 2018) | 1 line
Changed paths:
A /hello.html
Hello
------------------------------------------------------------------------
(2)svn diff
用來檢查歷史修改的詳情。
- 檢查本地修改
- 比較工作拷貝與版本庫
- 比較版本庫與版本庫
如果用 svn diff,不帶任何參數,它將會比較你的工作文件與緩存在 .svn 的"原始"拷貝。
[root@localhost test]# svn diff
Index: hello.html
===================================================================
--- hello.html (revision 3)
+++ hello.html (working copy)
@@ -1 +1 @@
-HelloWorld! http://www.test.com/test
+HelloWorld! http://www.test.com/test?123456
比較工作拷貝和版本庫
[root@localhost test]# svn diff -r 2 hello.html
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
Index: hello.html
===================================================================
--- hello.html (revision 2)
+++ hello.html (working copy)
@@ -1 +1 @@
-HelloWorld! http://www.test.com/tong
+HelloWorld! http://www.test.com/test?123456
比較版本庫與版本庫
[root@localhost test]# svn diff -r 1:2 hello.html
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
Index: hello.html
===================================================================
--- hello.html (revision 1)
+++ hello.html (revision 2)
@@ -1 +1 @@
-HelloWorld! http://www.test.com/
+HelloWorld! http://www.test.com/tong
(3)svn cat
如果只是希望檢查一個過去版本,不希望查看他們的區別,可使用svn cat
[root@localhost test]# svn cat -r 2 hello.html
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
HelloWorld! http://www.test.com/tong
(4)svn list
可以在不下載文件到本地目錄的情況下來察看目錄中的文件:
[root@localhost test]# svn list svn://192.168.1.69:3700/test
Authentication realm: <svn://192.168.1.69:3700> test
Password for 'admin':
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.1.69:3700> test
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? no
hello.html
