How to use Subversion from command line

Here we will see how to use Subversion from CLI (Command Line Interface).

As you may know, there are SVN clients like Tortoise for Windows or RabbitVCS for Linux, but in this post we will only cover CLI (my favourite way ;-)).

Initialize a project work copy in my local machine: svn checkout

To get a work copy of source files in their last version we need to execute a process to copy all the files in our file system, this is accomplished with the checkout command:

1
user@unix:$ svn checkout https://svn.hasheado.com/svn/hasheado/trunk hasheado-repo --username=hasheado

The above command will copy all files in our file system and all files are ready to be edited. Now, you can edit existing files, create new files and/or directories or remove files locally. Keep on mind that everything change we make, it only affects our local copy until we commit the changes to the svn repository.

Add files/directories: svn add

We can add a new file to the repository after create it and editing it in our local copy, or well, add a directory with or without content using the svn add command. This command will add the files/directories from your local copy and they will be added to the repo in the next commit. Also, we can change our minds and revert whatever we do not want using the svn revert command.

1
2
user@unix:$ svn add css/style.css
user@unix:$ svn revert

View file information: svn blame and svn cat

We can see the information about the author and revision number for specific files, using the svn blame FILENAME command; each line in the file is displayed at the beginning with the author (svn username) and revision number of the last change for the line.

If we want to see the differences in an specific file before to commit our changes, we can run the svn cat command.

Unlock a work copy

Sometimes we can see a “working copy locked” error. So, to unlock it, we can run:

1
user@unix:$ svn cleanup

Copy a file/directory: svn copy

Sometimes we have the need to just copy a file, to do this we can use the svn copy command as we can see below:

1
user@unix:$ svn copy SRC DST

Delete a file/directory: svn delete

If we want to delete a file or directory we can use:

1
user@unix:$ svn delete FILENAME

And to persist the deletion we need to commit.

Export a directory without .svn files: svn export

With this command we can extract a work copy with no version (without .svn files), to export a directory without .svn files we can run:

1
user@unix:$ svn export [-r REV] [PATH]

This will export a non-version work copy from the specified repository, we can specify the version as well, if no version is specified the head revision (HEAD) is exported. If the PATH is omitted, the last url is used to export.

1
user@unix:$ svn export PATH1 PATH2

SVN help

We can use the SVN help to see how to use every command:

1
user@unix:$ svn help [SUBCOMMAND...]

Send changes to repository

After we have made the changes locally in our files and/or directories, we should commit those changes to the repository.

Commit the changes: svn commit

To commit the changes to the repo:

1
user@unix:$ svn commit -m "Type your comment here" [files]

If we do not include any comment in the commit, we should add it in the default text editor which is triggered before SVN can complete the commit. All commits are logged into the SVN log.

Show commit logs: svn log

If we want to see the history of files or directories in our local copy or in the repo to track the revisions we can run:

1
user@unix:$ svn log [PATH]

The result is information about the files/directories, starting with the most current revision and showing information like the commit’s messages and authors.

Merge changes: svn merge

We can run the svn merge command to tell to Subversion that it should merge the last versions of the repo files into our local work copy.

Working with the repository

Create a new directory: svn mkdir

To create a new directory in your local work copy:

1
user@unix:$ svn mkdir PATH

Or, to create a new directory in the repo:

1
user@unix:$ svn mkdir URL

The end part of PATH or URL determines the new directory’s name. The new directory in the repo is created with an inmediate commit which requires a commit’s message.

Move a file/directory: svn move

We can move a file/directory using the svn move SRC DST command, with SRC being the source and DST the destiny. This command is equal to make a svn copy followed by a svn delete.

Resolve conflicts: svn resolved

In some situations we can get a conflict while we update our work copy. If this is the case, we need to resolve the conflict and then mark it as resolved. To do that we need to run:

1
user@unix:$ svn resolved PATH

SVN status

A good practice is to review our changes before to commit them, for that we can run svn status to print the file/directory status in our local copy.

Update our local copy: svn update

Another good practice is to update the local work copy every time we start working in the project, running:

1
user@unix:$ svn update [PATH...]

The updated items with their status are displayed as:
* A = A file was added to the local copy.
* U = A file was updated in our local copy.
* D = A file was deleted in our local copy.
* R = A file was replaced in our local copy.
* G = A file was successfully merged.
* C = A file has conflicts that we need to resolve.

Branching and Tagging

The project trunk is normally the main development thread, while the branches are used to different variants of that main development thread. For instance, when we want to build a new feature we can create a new branch to work on it. A tag is a way to group stable versions of the project. Although, the branches and tags are created using the svn copy command, their concepts are totally different. To create a branch/tag we have to run:

1
user@unix:$ svn copy SRC DST -m "Type your message here"
share it...Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Pin on PinterestDigg thisEmail this to someone

11 Comments

  1. @Pablo, like @David said, Git is an open-source version control system. I’m starting to play with it and looks amazing, I will post about it soon :-). Also, the biggest PHP project are starting to use it, like Symfony2, Doctrine, PHPUnit, etc.

    Reply
  2. Good post. I have a question, if I have a svn project named ‘Project1’ and I want to rename that project to ‘Project2’, what can I do to copy the project keeping the version? Thanks.

    Reply
    • @Jonathan: I had the same question than you, I tried to rename the folder with RabbitVCS and I couldn’t. Then I renamed it with F2 and there is no trouble to update and/or commit.

      Reply
  3. Excellent, it will be helpful to me. Could someone help me to configure the email notification when a user commits, please? I’ve tried many times and always I get an error besides I’ve read the Tortoise doc. The error is post-commit hook failed (exit code 255) with output: The syntax of the command is incorrect.

    Reply
  4. The only problem with Git is that you can’t have private projects with no pay and your repository is public for everyone, Otherwise with svn does not happen.

    Reply
    • @LinxMan: there are alternatives, like create your own git server, also, there are many git managers in the web, but of course, they could be no free since someone should be responsible for the files in the server.

      Reply
  5. Cool post @emiviada. But I have a question: we work without local copy due to a project requirement; many times we need to search for a file in the repository and we don’t know any client that allow us to search it through the repository without the file in our local copy. Do you know something? Thanks in advance

    Reply

Leave a Comment.