大多數情況下,我們日常工作中用的版本控制系統都會選擇分布式的Git,它相比於集中式的SVN有很多優勢。但是有些項目軟件基於自身限制,可能只支持SVN做工程同步。廢話就不多說了,下面記錄下SVN的部署和使用過程:
1)安裝SVN
[root@svn-server ~]# rpm -qa subversion [root@svn-server ~]# yum remove subversion [root@svn-server ~]# yum -y install subversion [root@svn-server ~]# svnversion --version 啟動svn,啟動時要指定svn的倉庫目錄 [root@svn-server ~]# mkdir -p /data/svn [root@svn-server ~]# /usr/bin/svnserve -d -r /data/svn [root@svn-server ~]# ps -ef|grep svn root 19826 1 0 10:52 ? 00:00:00 /usr/bin/svnserve -d -r /data/svn root 19829 19688 0 10:52 pts/1 00:00:00 grep svn [root@svn-server ~]# lsof -i:3690 #svn默認端口是3690 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME svnserve 19826 root 3u IPv4 12251011 0t0 TCP *:svn (LISTEN) 特別注意: svnserver的啟動命令要使用上面的"/usr/bin/svnserve -d -r /data/svn" 不要使用"service svnserve start"命令來啟動,否則會造成svn下載時報錯:svn: No repository found in 'svn://*.*.*.*/*' 設置開機啟動 [root@svn-server ~]# echo "/usr/bin/svnserve -d -r /data/svn" >> /etc/rc.local 停止和重啟SVN [root@svn-server ~]# killall svnserve [root@svn-server ~]# ps -ef|grep svn [root@svn-server ~]# /usr/bin/svnserve -d -r /data/svn [root@svn-server ~]# ps -ef|grep svn 如果已經有svn在運行,可以換一個端口運行 [root@svn-server ~]# /usr/bin/svnserve -d -r /data/svn --listen-port 3391 [root@svn-server ~]# lsof -i:3391 關閉防火牆,否則要打開3690端口 [root@svn-server ~]# /etc/init.d/iptables stop
2)代碼庫創建及配置
如下面創建兩個代碼庫,庫名為kevin和grace [root@svn-server ~]# svnadmin create /data/svn/kevin [root@svn-server ~]# svnadmin create /data/svn/grace [root@svn-server ~]# ls /data/svn/kevin/ conf db format hooks locks README.txt [root@svn-server ~]# ls /data/svn/grace/ conf db format hooks locks README.txt 配置代碼庫,這里以kevin代碼庫為例進行說明 [root@svn-server ~]# cd /data/svn/kevin/conf/ [root@svn-server conf]# ll total 12 -rw-r--r--. 1 root root 1080 May 31 10:59 authz #權限控制文件 -rw-r--r--. 1 root root 309 May 31 10:59 passwd #帳號密碼文件 -rw-r--r--. 1 root root 2279 May 31 10:59 svnserve.conf #SVN服務配置文件 設置該代碼庫的登錄帳號和密碼(由於是svn自己啟動的,沒有借助於apache啟動,所以這里的密碼是明文) [root@svn-server conf]# vim passwd ...... [users] # harry = harryssecret # sally = sallyssecret wangshibo = wangshibo@123 hanlili = hanlili@123 zhanghuan = zhanghuan@123 limin = limin@123 設置該代碼庫的操作權限 權限主體可以是別名,用戶組、用戶或*;別名在前面加&;用戶組在前面加@;*表示全部用戶; 權限可以是w、r、wr和空,空表示沒有任何權限。 [root@svn-server conf]# vim authz ..... [aliases] #設置別名 # joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average ops = wanghsibo,hanlili [groups] #設置組 # harry_and_sally = harry,sally # harry_sally_and_joe = harry,sally,&joe admin = wanghsibo,hanlili #創建一個admin組,將用戶加入到組 devha = zhuanghuan,limin # [/foo/bar] # harry = rw # &joe = r # * = # [repository:/baz/fuz] # @harry_and_sally = rw # * = r [/] #根目錄權限設置,用戶對kevin代碼庫根目錄的讀寫權限 wangshibo = rw hanlili = rw @devha = r [/haha/test] &ops = rw limin = rw [repository:/yunwei/kaixin] * = rw 修改svnserve.conf文件(在[general]區域添加下面四行內容) [root@svn-server conf]# vim svnserve.conf ..... [general] anon-access = none #匿名用戶可讀 auth-access = write #授權用戶可寫 password-db = passwd #使用哪個文件作為賬號文件。由於同在一個目錄路徑下,所以這里不用全路徑 authz-db = authz #使用哪個文件作為權限文件 realm = /data/svn/kevin #認證命名空間,subversion會在認證提示里顯示,並且作為憑證緩存的關鍵字 重啟svn [root@svn-server conf]# killall svnserve [root@svn-server conf]# ps -ef|grep svn root 20137 19688 0 11:41 pts/1 00:00:00 grep svn [root@svn-server conf]# /usr/bin/svnserve -d -r /data/svn [root@svn-server conf]# ps -ef|grep svn root 20139 1 0 11:41 ? 00:00:00 /usr/bin/svnserve -d -r /data/svn root 20141 19688 0 11:41 pts/1 00:00:00 grep svn
3)SVN客戶端常規操作命令總結
客戶機要安裝svn,確保有svn相關操作命令 [root@localhost ~]# yum install -y subversion ======================================================================= 查看svn的相關操作命令 [root@localhost svndata]# svn --help usage: svn <subcommand> [options] [args] Subversion command-line client, version 1.6.11. Type 'svn help <subcommand>' for help on a specific subcommand. Type 'svn --version' to see the program version and RA modules or 'svn --version --quiet' to see just the version number. Most subcommands take file and/or directory arguments, recursing on the directories. If no arguments are supplied to such a command, it recurses on the current directory (inclusive) by default. Available subcommands: add blame (praise, annotate, ann) cat changelist (cl) checkout (co) cleanup commit (ci) copy (cp) delete (del, remove, rm) diff (di) export help (?, h) import info list (ls) lock log merge mergeinfo mkdir move (mv, rename, ren) propdel (pdel, pd) propedit (pedit, pe) propget (pget, pg) proplist (plist, pl) propset (pset, ps) resolve resolved revert status (stat, st) switch (sw) unlock update (up) Subversion is a tool for version control. For additional information, see http://subversion.tigris.org/ ======================================================================= 客戶機下載svn代碼庫文件(192.168.10.202是上面svn服務端地址。即下面kevin代碼庫) 即從版本庫中導出 [root@localhost svndata]# svn checkout svn://192.168.10.202/kevin Authentication realm: <svn://192.168.10.202:3690> /data/svn/kevin Password for 'root': #首次需要輸入本機root密碼 Authentication realm: <svn://192.168.10.202:3690> /data/svn/kevin Username: wangshibo #輸入svn設置的用戶名,這里選擇wangshibo Password for 'wangshibo': #輸入wangshibo密碼 ----------------------------------------------------------------------- ATTENTION! Your password for authentication realm: <svn://192.168.10.202:3690> /data/svn/kevin 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)? yes Checked out revision 0. ======================================================================= 需要注意:也可以使用帶用戶名和密碼的訪問(svn co 等同於svn checkout): [root@localhost svndata]# svn co --username wangshibo --password wangshibo@123 svn://192.168.10.202/kevin ----------------------------------------------------------------------- ATTENTION! Your password for authentication realm: <svn://192.168.10.202:3690> /data/svn/kevin 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)? yes Checked out revision 0. [root@localhost svndata]# ls kevin [root@localhost svndata]# cd kevin/ [root@localhost kevin]# ll -a total 16 drwxr-xr-x. 3 root root 4096 May 31 14:41 . drwxr-xr-x. 3 root root 4096 May 31 14:17 .. drwxr-xr-x. 6 root root 4096 May 31 14:42 .svn +++++++++++++++++++++++++++++++++ 溫馨提示: svn checkout(即svn co)表示檢出。這樣下載到的svn代碼庫里包括.svn # svn co http://路徑(目錄或文件的全路徑) [本地目錄全路徑] --username 用戶名 --password 密碼 # svn co svn://路徑(目錄或文件的全路徑) [本地目錄全路徑] --username 用戶名 --password 密碼 # svn checkout http://路徑(目錄或文件的全路徑) [本地目錄全路徑] --username 用戶名 # svn checkout svn://路徑(目錄或文件的全路徑) [本地目錄全路徑] --username 用戶名 注意:如果不帶--password 參數傳輸密碼的話,會提示輸入密碼,建議不要用明文的--password 選項。 其中 username 與 password前是兩個短線,不是一個。 不指定本地目錄全路徑,則檢出到當前目錄下。 例子: svn co svn://192.168.10.202/kevin /data/svndata --username wangshibo --password wangshibo@123 svn co http://192.168.10.202/kevin --username wangshibo --password wangshibo@123 svn checkout svn://192.168.10.202/kevin /data/svndata --username wangshibo --password wangshibo@123 svn checkout http://192.168.10.202/kevin --username wangshibo --password wangshibo@123 ======================================================================= svn導出(導出一個干凈的不帶.svn文件夾的目錄樹) svn export [-r 版本號] http://路徑(目錄或文件的全路徑) [本地目錄全路徑] --username 用戶名 svn export [-r 版本號] svn://路徑(目錄或文件的全路徑) [本地目錄全路徑] --username 用戶名 svn export 本地檢出的(即帶有.svn文件夾的)目錄全路徑 要導出的本地目錄全路徑 注意: 第一種從版本庫導出干凈工作目錄樹的形式是指定URL,如果指定了修訂版本號,會導出相應的版本,如果沒有指定修訂版本,則會導出最新的,導出到指定位置。 如果省略 本地目錄全路徑,URL的最后一部分會作為本地目錄的名字。 第二種形式是指定 本地檢出的目錄全路徑 到 要導出的本地目錄全路徑,所有的本地修改將會保留,但是不在版本控制下(即沒提交的新文件,因為.svn文件夾 里沒有與之相關的信息記錄)的文件不會拷貝。 例子: 注意/opt/svndata目錄不能提前創建,下面導出命令執行后會自動創建該目錄 即把kevin版本庫里的所有文件都導出到本地的/op/svndata目錄下了,不包括.svn [root@localhost ~]# svn export svn://192.168.10.202/kevin /opt/svndata/ --username wangshibo --password wangshibo@123 A /opt/svndata A /opt/svndata/test.html Exported revision 7. [root@localhost ~]# ls /opt/svndata/ #如上,kevin版本庫里還沒有任何文件 [root@localhost ~]# cd /opt/svndata/ [root@localhost svndata]# ll -a #查看,發現導出后沒有帶.svn total 12 drwxr-xr-x. 2 root root 4096 May 31 14:52 . drwxr-xr-x. 4 root root 4096 May 31 14:52 .. ======================================================================= 添加新文件(svn add) 注:告訴SVN服務器要添加文件了,還要用svn commint -m真實的上傳上去! svn add test.php #添加test.php svn commit -m "添加我的測試用test.php" test.php #提交新加的文件到svn服務器里 svn add *.php #添加當前目錄下所有的php文件 svn commit -m "添加我的測試用全部php文件" *.php [root@localhost kevin]# echo "test123123" > test.html [root@localhost kevin]# svn add test.html A test.html [root@localhost kevin]# svn commit -m "this is test html" Adding test.html Transmitting file data . Committed revision 1. [root@localhost kevin]# mkdir haha [root@localhost kevin]# svn add haha A haha [root@localhost kevin]# svn commit -m "add haha" #"svn commit"可以簡寫成"svn ci" Adding haha Committed revision 2. [root@localhost kevin]# ls haha test.html ======================================================================= svn 提交 svn commit -m "提交備注信息文本" [-N] [--no-unlock] 文件名 svn ci -m "提交備注信息文本" [-N] [--no-unlock] 文件名 必須帶上-m參數,參數可以為空,但是必須寫上-m 例子: svn commit -m "提交當前目錄下的全部在版本控制下的文件" * #注意這個*表示全部文件 svn commit -m "提交我的測試用test.php" test.php svn commit -m "提交我的測試用test.php" -N --no-unlock test.php #保持鎖就用–no-unlock開關 svn ci -m "提交當前目錄下的全部在版本控制下的文件" * #注意這個*表示全部文件 svn ci -m "提交我的測試用test.php" test.php svn ci -m "提交我的測試用test.php" -N --no-unlock test.php #保持鎖就用–no-unlock開關 ======================================================================= svn更新操作。即把svn服務器上最新的版本更新下來 [root@localhost kevin]# svn update 或者"svn up" At revision 1. ======================================================================= svn查看 [root@localhost kevin]# svn info Path: . URL: svn://192.168.10.202/kevin Repository Root: svn://192.168.10.202/kevin Repository UUID: a5e3da23-8188-47af-afb7-fe4507492688 Revision: 1 Node Kind: directory Schedule: normal Last Changed Author: wangshibo Last Changed Rev: 1 Last Changed Date: 2018-05-31 14:21:46 +0800 (Thu, 31 May 2018) ======================================================================= svn刪除文件(簡寫svn del) svn delete svn://路徑(目錄或文件的全路徑) -m "刪除備注信息文本" 推薦如下操作: # svn delete 文件名 # svn ci -m "刪除備注信息文本" [root@localhost kevin]# svn delete haha #或者svn del haha D haha [root@localhost kevin]# svn commit -m "del haha" #或者svn ci -m "del haha" Deleting haha Committed revision 4. [root@localhost kevin]# ls test.html [root@localhost kevin]# svn delete svn://192.168.10.202/kevin/test.html -m "刪除測試文件test.html [root@localhost kevin]# svn update D haha D test.html Updated to revision 8. [root@localhost kevin]# ls [root@localhost kevin]# 注意:svn的刪除使用delete,而不是rm ======================================================================= svn查看日志 [root@localhost kevin]# svn log #顯示所有文件的所有修改記錄 ------------------------------------------------------------------------ r1 | wangshibo | 2018-05-31 14:21:46 +0800 (Thu, 31 May 2018) | 1 line this is test html ------------------------------------------------------------------------ [root@localhost kevin]# svn log test.html #顯示test.html這個文件的所有修改記錄,及其版本號的變化 ------------------------------------------------------------------------ r1 | wangshibo | 2018-05-31 14:21:46 +0800 (Thu, 31 May 2018) | 1 line this is test html ------------------------------------------------------------------------ ======================================================================= 版本庫下的文件和目錄列表 [root@localhost kevin]# svn ls test.html ======================================================================= 恢復本地修改 svn revert: 恢復原始未改變的工作副本文件 (恢復大部份的本地修改)。 revert: 用法: revert PATH... 注意: 本子命令不會存取網絡,並且會解除沖突的狀況。但是它不會恢復被刪除的目錄;也不會恢復已經經過svn commit提交過的文件 [root@localhost kevin]# cat test.html test123123 [root@localhost kevin]# echo "5555" >> test.html [root@localhost kevin]# cat test.html test123123 5555 [root@localhost kevin]# svn revert test.html Reverted 'test.html' [root@localhost kevin]# cat test.html test123123 ======================================================================= 加鎖/解鎖 svn lock -m “加鎖備注信息文本“ [--force] 文件名 svn unlock 文件名 例子: # svn lock -m “鎖信測試用test.php文件“ test.php # svn unlock test.php ======================================================================= 比較差異 svn diff 文件名 svn diff -r 修正版本號m:修正版本號n 文件名 例子: # svn diff test.php<- 將修改的文件與基礎版本比較 # svn diff -r 200:201 test.php<- 對 修正版本號200 和 修正版本號201 比較差異 ======================================================================= 查看文件或目錄狀態(簡稱svn st) # svn st 目錄路徑/名 # svn status 目錄路徑/名 目錄下的文件和子目錄的狀態,正常狀態不顯示 ?:不在svn的控制中; M:內容被修改;C:發生沖突;A:預定加入到版本庫;K:被鎖定 # svn -v 目錄路徑/名 # svn status -v 目錄路徑/名 顯示文件和子目錄狀態 第一列保持相同,第二列顯示工作版本號, 第三和第四列顯示最后一次修改的版本號和修改人 注意: svn status、svn diff和 svn revert這三條命令在沒有網絡的情況下也可以執行的,原因是svn在本地的.svn中保留了本地版本的原始拷貝。 [root@localhost kevin]# svn status -v test.html 1 1 wangshibo test.html ======================================================================= 解決沖突 # svn resolved [本地目錄全路徑] 例子: # svn update C foo.c Updated to revision 31. 如果你在更新時得到沖突,你的工作拷貝會產生三個新的文件: # ls foo.c foo.c.mine foo.c.r30 foo.c.r31 當你解決了foo.c的沖突,並且准備提交,運行svn resolved讓你的工作拷貝知道你已經完成了所有事情。 你可以僅僅刪除沖突的文件並且提交,但是svn resolved除 ======================================================================= 新建一個分支copy # svn copy branchA branchB -m "make B branch" 表示從branchA拷貝出一個新分支branchB ======================================================================= 合並內容到分支merge # svn merge branchA branchB 把對branchA的修改合並到分支branchB