There are two common ways to set up an SVN server on Windows.

One is to use VisualSVN Server, which is easier and more straightforward: http://www.visualsvn.com/server/download/

The other is the more traditional approach, using the standard Subversion server directly. This is the method often used in company environments, although it takes more manual setup.

Getting the tools ready

Download the Subversion server

Get the latest Subversion server package from the official site: http://subversion.apache.org/

Download the TortoiseSVN client

Get the latest TortoiseSVN client from: http://tortoisesvn.net/

TortoiseSVN is used on the client side to communicate with the Subversion server. Subversion itself also includes a command-line client, svn.exe, but TortoiseSVN is generally much more convenient to use and can improve day-to-day efficiency.

Installing the server and client

Install both the Subversion server and the client tools.

The server package is typically provided as a ZIP archive, so it can simply be extracted. For example, it can be unpacked to E:\subversion.

The client package is an executable installer. Run it and follow the prompts. After installation, the client may ask for a system restart.

Creating a repository

Before the server can be used, you need to create a repository. A repository is the central location on the server where versioned data is stored and managed.

Start by creating an empty folder such as E:\svn to serve as the root directory for all repositories.

Then open a command prompt, switch to the bin directory inside the Subversion installation, and run:

svnadmin create E:\svn\repos1

This creates a repository named repos1 under E:\svn. Subversion will automatically generate the necessary files and subdirectories inside it.

The same step can also be done graphically with TortoiseSVN.

First create an empty directory named E:\svn\repos1. It must be empty. Then right-click that folder and choose:

TortoiseSVN -> Create Repository here…

When prompted for a repository type, the default FSFS mode is fine. The result is the same as creating it from the command line.

Running the standalone server

At this point, only the repository exists; the SVN service is not yet running. In the same command window, enter:

svnserve.exe --daemon

By default, svnserve listens on port 3690. The --daemon option tells it to keep running in the background until manually stopped.

One thing to watch for: if you launch it from a command window this way, closing that window will stop svnserve.

To verify that the server is working, open TortoiseSVN and use Repo-browser. In the URL field, enter:

svn://localhost/svn/repos1

If everything is working, you should be able to browse the repository tree. At this stage it will still be empty.

You can also use the --root option to restrict access to a specific repository root. This is both safer and more convenient because it shortens repository URLs:

svnserve.exe --daemon --root drive:\path\to\repository

Using the example above, the command would be:

svnserve.exe --daemon --root e:\svn

Then the Repo-browser URL becomes shorter:

svn://localhost/repos1

Starting SVN automatically with Windows

One approach is to register svnserve as a Windows service. A batch file such as autoRunSVN.bat can be created with the following content:

sc create MySVNServer binpath= ""C:\Program Files\Subversion\svnserve\svnserve" --service -r H:\home\repos" displayname= "SVNService" depend= Tcpip start= auto
pause

sc create MySVNServer binpath= ""J:\java\Subversion\bin\svnserve.exe" --service -r "G:\svn repository"" displayname= "SVNService" depend= Tcpip start= auto
pause

A few details matter here:

  1. sc is the Windows built-in service configuration tool. MySVNServer is the service name.
  2. binPath points to the svnserve executable. If the path contains spaces, such as Program Files, it must be wrapped in double quotes. Because double quotes are special characters in this context, they need to be escaped as shown.
  3. --service tells svnserve to run as a Windows service, and -r specifies the repository location. These parameters are part of binPath together with the executable path.
  4. displayname is the name shown in the Windows Services list. depend= Tcpip means the service depends on TCP/IP. start= auto enables automatic startup when Windows boots.
  5. The syntax is strict: there must be no space before the equals sign, but there must be a space after it. This applies to binPath=, displayname=, depend=, and start=.
  6. --service uses a double hyphen, while -r uses a single hyphen.
  7. To remove the SVN service, run:
sc delete svnserve
  1. Everything from sc through auto must stay on a single line.

After the service is created, you can open services.msc and look for SVNService in the Windows Services console, then manage it from there.

In practice, this startup method may not always work as expected. Another tested command that did successfully start automatically was:

sc create svnserver binpath= "D:\Program Files\svnsoft\subversion\bin\svnserve.exe --service --root D:\svnroot\repos1" displayname= "svnserver" depend= tcpip start= auto obj= "NT AUTHORITY\NetworkService"

That version was able to start automatically.

Another option is to use a helper batch script to create the service and generate separate scripts for starting, stopping, and deleting it. Create SVNService.bat with the following content:

@ECHO OFF

::======================================================================
::作者     : Zealic
::版本     : 1.05
::功能     : 通过简便快捷的方式安装 Subversion.
::最后更新 : 2007-12-28
::======================================================================
::初始化环境
::======================================================================

ECHO *** 安装 Subversion 服务 ***

::======================================================================
::======================================================================
::创建服务
::======================================================================
ECHO +++ 创建资源库服务 +++
ECHO 创建系统服务,这需要输入一些信息...

:INPUT_SVC_NAME
SET /P svc_name=请输入服务名     :
::服务名不能为空值
if "%svc_name%" == "" (
ECHO 服务名不能为空,请重新输入。
GOTO INPUT_SVC_NAME
)
::查询服务是否存在,通过查找输出是否存在错误,"1060:"是发生错误是时输出的错误代码。
SC QUERY "%svc_name%" | find "1060:" >> NUL
IF %errorlevel% == 1 (
ECHO 服务 "%svc_name%" 已经存在!请重新输入服务名。
GOTO INPUT_SVC_NAME
)
ECHO.

::输入资源库信息
:INPUT_REPOS
SET /P    repos=请输入资源库路径 :
ECHO.

::创建服务
ECHO 您使用以下信息创建 Subversion 服务 :
ECHO 服务名称   : "%svc_name%"
ECHO 资源库路径 : "%repos%"
ECHO 正在创建服务...
SC create %svc_name% binPath= ""E:\Subversion\bin\svnserve.exe" --service -r "%repos%"" depend= Tcpip start= auto| find "1060:" >> NUL
ECHO.
IF %errorlevel% == 0 GOTO SC_ERROR
ECHO --- 创建服务成功! ---
ECHO.
ECHO.

::======================================================================
::生成操作批处理文件
::======================================================================
ECHO +++ 生成服务操作批处理 +++
ECHO 服务操作批处理文件可以帮助您快速操作服务。
SET bat_delete=DELETE_%svc_name%.BAT
SET bat_start=START_%svc_name%.BAT
SET bat_stop=STOP_%svc_name%.BAT
ECHO 生成删除服务批处理 "%bat_delete%" ...
::bat_delete
ECHO @ECHO OFF> %bat_delete%
ECHO NET STOP %svc_name%>> %bat_delete%
ECHO SC delete %svc_name%>> %bat_delete%
ECHO DEL %bat_start%>> %bat_delete%
ECHO DEL %bat_stop%>> %bat_delete%
ECHO DEL %bat_delete%>> %bat_delete%
ECHO PAUSE>> %bat_delete%
::bat_start
ECHO 生成运行服务批处理 "%bat_start%" ...
ECHO @ECHO OFF> %bat_start%
ECHO NET START %svc_name%>> %bat_start%
ECHO PAUSE>> %bat_start%
::bat_stop
ECHO 生成停止服务批处理 "%bat_stop%" ...
ECHO @ECHO OFF> %bat_stop%
ECHO NET STOP %svc_name%>> %bat_stop%
ECHO PAUSE>> %bat_stop%
ECHO.
ECHO --- 生成服务操作批处理成功! ---
ECHO.
ECHO.

::======================================================================
::Finish
::======================================================================
ECHO ### 所有操作成功! ###
GOTO END

::======================================================================
::错误段
::======================================================================
:ENV_ERROR
ECHO --- 设置环境变量失败! ---
GOTO END

:SC_ERROR
ECHO --- 创建服务失败! ---
GOTO END

:SC_INV_PORT
ECHO 无效的端口号,请重新输入。
GOTO INPUT_SVC_PORT

:END
PAUSE

The key line in that script is:

SC create %svc_name% binPath= ""E:\Subversion\bin\svnserve.exe" --service -r "%repos%"" depend= Tcpip start= auto| find "1060:" >> NUL

You need to change binPath so it matches the actual location of svnserve.exe on your machine. After that, the service can be run normally.

Configuring users and access

Open the conf directory inside the repository, for example:

E:\svn\repos1\conf

Edit svnserve.conf and change:

# password-db = passwd

to:

password-db = passwd

That means removing the leading # so the setting is enabled. Make sure there is no extra space before it.

Then edit the passwd file in the same directory and add a user account.

Original content:

[users]
# harry = harryssecret
# sally = sallyssecret

Add an account like this:

[users]
#harry = harryssecret
#sally = sallyssecret
test = test

Importing an existing project

Once the repository is ready, the next step is to import an existing project into it so that all future changes are tracked by SVN.

For example, suppose there is a folder named guestbook under d:\wwwroot, containing a message board application.

Right-click the guestbook folder and choose:

TortoiseSVN -> Import…

In the URL of repository field, enter:

svn://localhost/repos1/guestbook

In Import message, enter something like 导入整个留言簿 as the commit note.

When prompted for credentials, use the username and password configured earlier, for example test / test.

After the import finishes, all contents of guestbook will have been stored in the repository.

A common point of confusion is that you will not see an actual guestbook folder appear inside e:\svn\repos1 in Windows Explorer. The repository’s internal storage does not mirror the project structure in a normal file-system layout. What you may notice is simply that the repository folder has grown in size.

At that point, the original source folder can be deleted if needed.

This import step does not have to be done on the same machine that runs svnserve. It can be performed from any client computer with TortoiseSVN installed. For example, if the SVN server machine has the IP address 133.96.121.22, the repository URL would use:

svn://133.96.121.22

Basic daily workflow

Check out a working copy

To start working with files under version control, check out a working copy from the repository.

Create an empty folder anywhere, for example f:\work. Right-click it and choose SVN Checkout. In the repository URL field, enter:

svn://localhost/svn/repos1/guestbook

This creates a working copy containing the contents of guestbook.

Commit changes back to the repository

Make changes inside the working copy, then submit them to the repository.

For example, open any file in the checked-out guestbook working copy, edit it, then right-click and choose SVN Commit…

That sends the modification back to the repository, where SVN records the change as a new revision.

To review the history of a changed file, right-click it and choose:

TortoiseSVN -> Show Log

This displays all commits made to that file. From any revision entry, you can right-click and select Compare with working copy to see the differences between your current local version and the selected revision.