Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
This chapter is from the book

4.8 Creating a Branch

You should have tags pretty well down at this point, so let's take a look at branches. Say, for example, that your boss isn't yet quite as enlightened as you are, and decides you need to release a version of Hello World that touts that other version control system. Because you know he's heading down a dead-end path, though, you don't want to stop development on your already excellent version of Hello World. The solution is to create a branch of the project, which will allow you to take the project in a different direction, while maintaining the current development path in parallel.

Branches in Subversion are just like tags, copies of the original repository part they refer to. Therefore, you make them exactly the same way; only in this case, you will want to copy the files into the branches directory, instead of the tags directory.

$ svn copy --message "Created a branch of the project to make the boss happy" file:///home/bill/my_repository/trunk/ file:///home/bill/my_repository/branches/cvs_version

Committed revision 4.

After you have created the branch, you'll need to put it into a working copy so that you can make changes to it. You could check out the branch (using svn checkout) into a new working copy. In fact, that will work just fine. There's a better solution, though. Instead of checking out a new working copy, you can switch your current working copy to point to the branch, instead of the /trunk directory that it points to now. To do this, you need to use the svn switch command. To switch your working copy to the branch, run the following command line.

$ svn switch file:///home/bill/my_repository/branches/cvs_version
Updated to revision 4.

The files in your working copy now point to the branches/cvs_version/ directory, and any changes that you commit will be applied to that directory. In this particular case, running svn switch didn't make any changes to the files in your working copy, because the branch and your trunk are identical. Had they been different, Subversion would have updated all of your working copy files to reflect the cvs_version/ directory that you switched to.

You can look at what directory you are currently switched to by running svn info. For instance, the following command line will show you that your current working copy is switched to the cvs_version branch (look at the URL line).

$ svn info
Path: .
URL: file:///home/bill/my_repository/branches/cvs_version
Repository UUID: 5380c965-27ea-0310-9e69-9d7dd738c2c1
Revision: 4
Node Kind: directory
Schedule: normal
Last Changed Author: bill
Last Changed Rev: 4
Last Changed Date: 2004-12-01 00:46:13 -0500 (Wed, 01 Dec 2004)

Now that you have switched your working copy to point to the branch, you'd probably like to make some changes to the branch. For instance, to make your boss happy, you might change my_repository/branches/cvs_version/hello.c to look like this:

#include <stdio.h>

int main(int argc, char** argv)
    printf("CVS is the best!!\n"); // Ugh! The boss made me do it

    return 0;

Then, when you commit those changes, they will be applied to the copied version of the file, but the original file will remain unaffected, as you can see in the log outputs here.

$ svn commit --message "Changed program output to praise CVS"
Sending        hello.c
Transmitting file data .
Committed revision 5.

After the commit, the branch shows the committed change.

$ svn log file:///home/bill/my_repository/branches/cvs_version/hello.c
r5 | bill | 2004-07-12 23:32:11 -0500 (Mon, 12 Jul 2004) | 1 line

Changed program output to praise CVS
r4 | bill | 2004-07-12 22:47:11 -0500 (Mon, 12 Jul 2004) | 1 line

Created a branch of the project to make the boss happy
r2 | bill | 2004-07-11 04:45:12 -0500 (Sun, 11 Jul 2004) | 1 line

Changed program output
r1 | bill | 2004-07-08 16:28:57 -0500 (Thu, 08 Jul 2004) | 1 line

Initial import

But the the original hello.c file, still only shows the first two revisions.

$ svn log file:///home/bill/my_repository/trunk/hello.c
r2 | bill | 2004-07-11 04:45:12 -0500 (Sun, 11 Jul 2004) | 1 line

Changed program output
r1 | bill | 2004-07-08 16:28:57 -0500 (Thu, 08 Jul 2004) | 1 line

Initial import

Of course, now that you're done modifying the branch, it's a good idea to switch your working copy back to the trunk. If you don't make the switch as soon as you're done with the branch, it can be all too easy to forget and accidentally apply modifications to the wrong place.

$ svn switch file:///home/bill/my_repository/trunk/
U hello.c
Updated to revision 5.

As you can see, Subversion updates your hello.c file so that it represents the trunk version, rather than your modified branch version of the file.

  • + Share This
  • 🔖 Save To Your Account