第1章 CentOS下搭建SVN服務器
1.1 SVN簡介
SVN是Subversion的簡稱,是一個開放源代碼的版本控制系統,相較於RCS、CVS,它采用了分支管理系統,它的設計目標就是取代CVS。互聯網上很多版本控制服務已從CVS遷移到Subversion。說得簡單一點SVN就是用於多個人共同開發同一個項目,共用資源的目的
1.2 SVN安裝
官網下載: http://subversion.apache.org/packages.html
SVN客戶端TortoiseSVN :https://tortoisesvn.net/downloads.html
1.2.1 系統環境
[root@Alex ~]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) [root@Alex ~]# uname -r 3.10.0-229.el7.x86_64 [root@Alex ~]# systemctl status firewalld firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled) Active: inactive (dead) Oct 30 22:13:43 Alex systemd[1]: Starting firewalld - dynamic firewall daemon... Oct 30 22:13:48 Alex systemd[1]: Started firewalld - dynamic firewall daemon. Oct 30 22:44:11 Alex systemd[1]: Stopping firewalld - dynamic firewall daemon... Oct 30 22:44:12 Alex systemd[1]: Stopped firewalld - dynamic firewall daemon. [root@Alex ~]# getenforce Permissive
1.2.2 安裝
使用yum命令安裝svn
$ yum install subversion -y
新建svn存儲目錄
$ mkdir /svn
新建一個測試倉庫
$ svnadmin create /svn/test/ $ ll /svn/test/ total 16 drwxr-xr-x. 2 root root 51 Oct 31 12:45 conf drwxr-sr-x. 6 root root 4096 Oct 31 12:45 db -r--r--r--. 1 root root 2 Oct 31 12:45 format drwxr-xr-x. 2 root root 4096 Oct 31 12:45 hooks drwxr-xr-x. 2 root root 39 Oct 31 12:45 locks -rw-r--r--. 1 root root 229 Oct 31 12:45 README.txt
以下關於目錄的說明:
hooks目錄:放置hook腳步文件的目錄
locks目錄:用來放置subversion的db鎖文件和db_logs鎖文件的目錄,用來追蹤存取文件庫的客戶端
format目錄:是一個文本文件,里邊只放了一個整數,表示當前文件庫配置的版本號
conf目錄:是這個倉庫配置文件(倉庫用戶訪問賬戶,權限)
1.2.3 配置SVN配置文件
$ cd /svn/test/conf $ vim svnserve.conf ### This file controls the configuration of the svnserve daemon, if you ### use it to allow access to this repository. (If you only allow ### access through http: and/or file: URLs, then this file is ### irrelevant.) ### Visit http://subversion.tigris.org/ for more information. [general] ### These options control access to the repository for unauthenticated ### and authenticated users. Valid values are "write", "read", ### and "none". The sample settings below are the defaults. anon-access = read ##注意前邊不要有空格,要頂齊 auth-access = write ##注意前邊不要有空格,要頂齊 ### The password-db option controls the location of the password ### database file. Unless you specify a path starting with a /, ### the file's location is relative to the directory containing ### this configuration file. ### If SASL is enabled (see below), this file will NOT be used. ### Uncomment the line below to use the default password file. password-db = passwd ##注意前邊不要有空格,要頂齊 ### The authz-db option controls the location of the authorization ### rules for path-based access control. Unless you specify a path ### starting with a /, the file's location is relative to the the ### directory containing this file. If you don't specify an ### authz-db, no path-based access control is done. ### Uncomment the line below to use the default authorization file. authz-db = authz #授權文件 ### This option specifies the authentication realm of the repository. ### If two repositories have the same authentication realm, they should ### have the same password database, and vice versa. The default realm ### is repository's uuid. realm = This is My First Test Repository ##這個是提示信息 [sasl] ### This option specifies whether you want to use the Cyrus SASL ### library for authentication. Default is false. ### This section will be ignored if svnserve is not built with Cyrus ### SASL support; to check, run 'svnserve --version' and look for a line ### reading 'Cyrus SASL authentication is available.' # use-sasl = true ### These options specify the desired strength of the security layer ### that you want SASL to provide. 0 means no encryption, 1 means ### integrity-checking only, values larger than 1 are correlated ### to the effective key length for encryption (e.g. 128 means 128-bit ### encryption). The values below are the defaults. # min-encryption = 0 # max-encryption = 256
1.2.4 配置訪問用戶及密碼
$ cd /svn/test/conf $ vim passwd ### This file is an example password file for svnserve. ### Its format is similar to that of svnserve.conf. As shown in the ### example below it contains one section labelled [users]. ### The name and password for each user follow, one account per line. [users] # harry = harryssecret # sally = sallyssecret dev = 123456 test1 = 123456 test2 = 123456
1.2.5 配置新用戶的授權文件
$ cd /svn/test/conf ### This file is an example authorization file for svnserve. ### Its format is identical to that of mod_authz_svn authorization ### files. ### As shown below each section defines authorizations for the path and ### (optional) repository specified by the section name. ### The authorizations follow. An authorization line can refer to: ### - a single user, ### - a group of users defined in a special [groups] section, ### - an alias defined in a special [aliases] section, ### - all authenticated users, using the '$authenticated' token, ### - only anonymous users, using the '$anonymous' token, ### - anyone, using the '*' wildcard. ### ### A match can be inverted by prefixing the rule with '~'. Rules can ### grant read ('r') access, read-write ('rw') access, or no access ### (''). [aliases] # joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average [groups] # harry_and_sally = harry,sally # harry_sally_and_joe = harry,sally,&joe # [/foo/bar] # harry = rw # &joe = r # * = # [repository:/baz/fuz] # @harry_and_sally = rw # * = r admin = dev,test1 user = test2 [/svn/test/] @admin = rw @user = r * = r
說明:
[repo0:/] 代表對repo0所有版本庫設置權限權限
[repo0:/21yunwei] 代表對repo0版本庫下的21yunwei項目設置權限
[repo0:/21yunwei/demo] 代表對repo0版本庫下的21yunwei項目的demo目錄設置權限
admin = lqb,test2 創建admin組,組成員為:lqb,test2
user = test1 創建用戶組,用戶成員:test1
[test:/] 賦予根權限,為了便於管理和權限的控制,可以把權限細化到版本庫中相應的目錄,如[test:/test]這就是test項目下的一個目錄
@admin = rw admin組有讀寫的權限
@user = r user組只有讀的權限
*= 表示除了上面設置的權限用戶組以外,其他所有用戶都設置空權限,空權限表示禁止訪問本目錄,這很重要一定要加上
1.2.6 啟動svn
$ svnserve -d -r /svn
注意:更改svnserver.conf時需要重啟SVN服務,更改authz,passwd文件時則不需要重啟服務
第2章 通過客戶端進行連接
2.1 下載客戶端並安裝
首先安裝SVN客戶端,windows一般選擇烏龜客戶端https://tortoisesvn.net/downloads.html。
根據系統位數選擇相應客戶端進行安裝
如果你喜歡用命令行操作,請務必記得勾選command line client tool
為will be install on local hard driver
,不用命令行的跳過這一步
然后一路next即可安裝。安裝完畢后,在任意地方右鍵查看快捷菜單。發現TortoiseSVN即表示安裝成功。
如果勾選了安裝命令行工具,那么輸入命令SVN,有如下提示也表示安裝成功
2.2 中文設置
但是此時菜單全是英文的,如果你不習慣英文,可以去下載語言包,記得下對系統位數
官網下載,上面有地址
安裝完語言包之后,可以右鍵進入setting
設置
選擇你喜歡的語言(比如中文),然后確定,不出意外,現在的語言已經切換到中文了
安裝教程到此結束,下面開始介紹SVN的使用
2.3 使用說明
2.3.1 檢出項目
假如項目已經在服務器的倉庫里,那么現在你要做的就是把它檢出到本地。
首先創建一個空文件夾。在空文件夾內右鍵,選擇SVN檢出
現在你看到應該是這個界面,填入版本庫地址,選擇確定
此時會彈出一個對話框讓你輸入賬號密碼,輸入你的賬號密碼即可。記得勾選保存認證,不然每次操作都會讓你輸入。
等幾分鍾就可以檢出完畢
此時在你的目錄下就能看到你的項目,現在可以開始愉快的工作了
2.3.2 導入項目
但是有時候你已經在本地建立好了項目,需要把你項目推到SVN上,此時應怎么做呢?
右鍵選擇版本庫瀏覽器。
在相應目錄下,右鍵,加入文件/加入文件夾,選擇相應目錄即可
比如我現在有個項目叫SVNProject,我想把它傳到SVN上
那么我只需選擇加入文件夾即可。
務必要輸入提交信息。這樣別人才能知道你干了什么
導入成功就能看到目錄。
但是,不要以為導入成功就可以了。你還得重新檢出,重新檢出的項目才是受SVN控制的,務必記得檢出
在SVNProject上右鍵檢出到本地,然后在里面進行修改。現在就可以愉快的工作了。
檢出過后的右鍵菜單變成了這樣。
綠色表示當前文件沒有被修改過(看不見顏色的重啟下電腦就好了)。
假如我現在在我是新項目
.txt
中加了一行字,然后保存
發現現在變成了紅色,紅色表示已修改
怎么提交修改?
在根目錄下,右鍵選擇提交。
務必記得輸入提交信息(雖然不輸入也能提交),提交信息可以方便日后查看。
提交完畢后,可以發現又恢復到了綠色
假如現在加入了一個新文件。可以看出是藍色的。藍色表示不屬於版本庫的未知文件,未知文件是不能提交的。
記住選擇增加把它加入到版本庫里面去
增加完畢后,變成了藍色加號,表示新增加的版本庫文件
接下來,只需寫代碼,然后提交即可。
刪除文件也應該右鍵提交,如下。
記得隨時檢查你的文件狀態,如果沒有添加到版本控制里要及時添加進去,不然你的文件提交不上去。
2.3.3 更新
假如你和B同學在協作。B同學寫完代碼提交到了SVN上,如果你想獲取最新修改,就需要選擇更新(如果服務器上已經有別人提交過的新的,你是提交不上去的,必須先更新再提交)。
怎么知道服務器有沒有更新?你可以直接選擇更新,有沒有更新一下就知道。或者右鍵檢查修改,然后檢查版本庫,就能看到服務器上改了哪些文件
右鍵選擇HEAD和BASE比較。
左邊的表示你的代碼,右邊的表示服務器上的代碼
如果有修改記得及時更新到本地然后再繼續工作
但是有時候更新會沖突,比如你和服務器上的改了同一個地方。
這時候你需要更新下來解決沖突。
它會提示你哪個文件沖突,你只需打開那個文件,按照需求解決沖突即可
<<<<<<.mine到====表示你的代碼,其他表示服務器的代碼。你只需改成你想要的
然后選擇解決,告訴SVN我已經解決沖突了就行了
剩下的就是團隊協作間的更新提交操作,這里不做贅述
2.3.4 查看日志
選擇顯示日志,可以看出團隊里面的人干了什么
可以看出誰誰誰,什么時間,干了什么事。最后那一列信息是自己提交的時候寫的。建議大家提交時務必要填寫提交信息,這樣別人一看就知道你干了什么。提交信息對於自己也是有好處的,時間長了也能看到當初做了什么。
2.3.5 版本回滾
如果你改了東西,但是還沒有提交,可以使用還原功能。
但是如果我們寫錯了東西並且提交了上去怎么辦?通過版本回滾可以將文件恢復到
以前的版本。右鍵更新至版本,通過查看日志來選擇版本,然后回滾即可
有時候我們需要查看以前版本的代碼。此時我們可以新建個文件夾檢出到指定版本
2.3.6 版本控制
版本控制有好幾種方法,如下。
在提交發布版本時添加版本信息,這是最簡單的一種方法。
打標簽
每次發布版本時應該打標簽。右鍵選擇分支/標記。在至路徑以版本號打上標簽即可
這樣你就有了一個v1.0版本的標簽。
以后如果你想查看某個版本的代碼,只需切換過去就行
部分摘自這位大佬的博客:https://blog.csdn.net/maplejaw_/article/details/52874348