Working with SVN – how to start?

svnSo you need to use subversion and you have no idea where to start? I will try to help you by providing you a basic knowledge with some examples. In this post you will learn how to get SVN repository from server to your local machine, make changes and send it back to the server.

Local SVN copy

First what you need to do is to create a local directory and get SVN files (even if it is totally new project you need to get SVN directory structure).

So let's create your local directory:

$ mkdir local-project-dir

Now get files and directory structure from remote reprository (you need to do it only once):

$ svn co local-project-dir
> A	local-project-dir/trunk
> A	local-project-dir/branches
> A	local-project-dir/tags
> Checked out revision 12431.

If project is not new it is very possible you will get much more files and directories. "A" at the begging tells us that listed directories have been added.

Checking your local copy

You have a local copy. Probably now you will want to make some changes or add your own files.

If it took some time since you made your local copy you may first check for changes:

$ cd local-project-dir
$ svn up
> At revision 12431.

It looks that our local copy is up to date. If there are any remote changes it would sync it now.

Adding new files

To add new files to the repository you should first copy it to trunk folder.

local-project-dir/$ cp ~/my-file.php trunk/
local-project-dir/$ cp ~/another-file.php trunk/

Now you need to let subversion know about your new files:

local-project-dir/$ svn add trunk/*
> A trunk/my-file.php trunk/
> A trunk/another-file.php trunk/

And now you need to commit changes to remote repository :

local-project-dir/$ svn ci -m "Adding files with feature x and y"
> Adding	trunk/my-file.php
> Adding	trunk/another-file.php
> Transmitting file data .
> Committed revision 12432.

Editing files

To edit any files you must make sure that your local copy is up to date. So first you should check your copy as described before. Then you can edit any files in "trunk" subfolder (or add new). When you are ready you may check all changes against remote repository:

local-project-dir/$ svn stat
> M	trunk/my-file.php

It looks than SVN see our changes in "my-file.php". "M" on the beggining of the line is for "Modified". You may see exact changes by making "svn diff" command. Now you need to commit your changes as with adding new files:

local-project-dir/$ svn ci -m "Fixed feature x and y"
> Sending	trunk/my-file.php
> Transmitting file data .
> Committed revision 12433.

Removing files

You can not just remove files from trunk folder as it would return errors when you try to commit changes. You need to do it with help of svn. If you want to remove files that have already been committed to remote repository you need to make a command:

local-project-dir/$ svn rm trunk/my-file.php
> D	trunk/my-file.php

And then commit changes as described previously.

If files have already been added to your local SVN copy but have not been committed you will need to use --force switch:

local-project-dir/$ svn rm trunk/my-file.php --force
> D	trunk/my-file.php

And that's all! Simple! Isn't it? Now you have basic knowledge about subversion usage. If you want more details check in subversion online book or ask your questions in comments.

1 Responses to “Working with SVN – how to start?”

  • Thanks for providing this useful information its very informative to work with SVN,nice blog.I will use them and let you know the results.keep posting theses kinds of stuffs!!

Leave a Reply