天天看點

SVN使用—常用指令及避免沖突的方法

本文介紹svn的常用指令及避免沖突的方法

一、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可以為多個版本庫工作

[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
           

繼續閱讀