Creating Tags in Subversion

Subversion does not support labels as many version control tools do. Instead Subversion uses the svn copy command to create ‘tags’.
By convention a Subversion repository is often divided into three sub-directories;

  • trunk, where the main development is often (though not necessarily) done;
  • branches, where (unsurprisingly) we maintain branches that may be used for any number of reasons to isolate some sequence of changes from the trunk;
  • tags, where we maintain tagged sets of files and directories.

It is this last area tags that this post considers in more detail.

Creating a basic tag

Creating a simple tag is incredibly fast and simple. Simply copy the thing to be tagged into the tags directory.

svn copy http://subversion/myrepo/trunk http://subversion/myrepo/tags/release_1.0

This command creates a copy of the current revision of the trunk in tags/release_1.0. Creating this copy is very quick and requires almost no resources in the repository’s virtual file system*.

Selectively tagging

So, creating a straightforward tag is simple but what about more complex tags? What if we want to tag only selected files and directories within our trunk?

There are many ways that this can be achieved. We will examine just one of these methods here*. In this example we will create a new working copy for the tag and manually populate it with items from the repository. This method is suitable when using Subversion 1.5+ and provides complete control over what is tagged and how it is recorded in the tag.

First we need to create a working copy to hold the tag. Again, there are several methods of doing this. The method shown here allows the entire tag to be created as a single transaction (more closely emulating the creation of simple tags).

svn co --depth=empty http://subversion/myrepo/tags
svn mkdir tags/mynewtag

These first two commands checkout the tags directory (but none of the existing tags are checkout because we have specified –depth=empty. The mkdir command then creates an empty directory mynewtag in the tags working copy.

We have not yet created a tag in the repository’s virtual file system, but we have prepared a working copy which, when committed will do just that.

Having prepared our working copy we now copy items into it. In this example we are copying items from the repository into the working copy.

cd tags/mynewtag
svn copy http://subversion/myrepo/trunk/file1.txt .
svn copy --parents http://subversion/myrepo/trunk/dir1/file2.txt dir1/file2.txt

Notice that when copying files into nested directories I have chosen to preserve the directory structure. Using the –parents option ensures that all the necessary intermediate directories are created and scheduled for addition as part of the copy operation.

We do not have to preserve the directory structure.

svn copy http://subversion/myrepo/trunk/dir1/file3.txt file3.txt

This command will create a tagged version of the dir1/file3.txt in tags/mynewtag/file3.txt. Similarly we can rename files, or assign them into other directories.

We can also tag complete sub-directories just as simply.

svn copy http://subversion/myrepo/trunk/dir2 dir2

This tags the whole trunk/dir2 as tags/mynewtag/dir2.

Using the –revision option we can control precisely which revisions are tagged (the default being HEAD).

svn copy --revision 4 http://subversion/myrepo/trunk/dir3 dir3

This command tags the revision 4 of trunk/dir3 as mynewtag/dir3.

Once we are happy that our mynewtag working copy contains all of the items we want in the tag, we commit the entire working copy to create the tag in the repository.

cd ..
svn commit -m "Create mynewtag" .

* If you found this post useful and are keen to learn more about Subversion, you may be interesting in my Subversion Client training course.
Or, for more answers to Subversion questions, try my Subversion FAQ.

About these ads

, ,

  1. #1 by Greg on January 5, 2010 - 11:36 am

    Interesting post indeed. I’m not used to create tags this way, so thank you for the tip. A few things that your blog entry is missing in order to be complete:
    - Tags = Branches = directories. That’s an important thing that most subversion user don’t know about. There is absolutly no magic behind tags and branches.
    - it should be told that selective tags can be created directly into subversion which avoid the “cost” of having a working copy in order to create tags. EG this is perfectly valid: svn copy –revision 4 http://subversion/myrepo/trunk/dir3 http://subversion/myrepo/tags/mynewtag/dir3 (provided that mynewtag folder already exists).
    - One really usefull feature of subversion 1.6 which is particularly convenient when creating tags is the relative urls thing. Provided that you are located in a Working Copy, it let you write something like this:
    svn copy ^/trunk ^/tags/release-1.0 -m “Tagging for release 1.0″
    You can find more about relatives urls in subversion here: (a bit of self-promotion ;-)).


    • #2 by Mark Bools on January 5, 2010 - 12:18 pm

      Thanks Greg. Your points are well made. The new 1.6 relative URLs are a massive help doing this sort of thing.

      This brief post was, of course incomplete, for a more complete treatment of tagging check out my training course (you’re not the only one who can self promote :D ).

      While we’re on the subject, I would encourage people to join the WANDisco Subversion effort ( it’s only just starting but is shaping up nicely as a focus for Subversion related information.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: