Branching and Merging Code in CVS

CVS has never been a configuration management system of my choice but none the less there are times when it doesn’t come down to my choice, I recently had to branch some code as I had to start on a new set of developments where as the previous development hadn’t been released so I had to save the snapshot of the code and carry on with the development for the next release. Not ideal! but that was the case.

Documenting this for now so that I can refer to it later and for anyone else who might find it useful.

DISCLAIMER: I have always found working with CVS very daunting and hence I am always extra careful in running the commands. The following instructions work for me and I cannot guarantee how it will work for you. So execute the commands only if you understand the instructions and are sure what you are doing.

Go to working directory and make sure you have the most recent version of the code:

cvs up -A

Do a cvs stat on any file to ensure that you do have any sticky bits set, from a previous branch etc.

$ cvs stat stuff.xml
============================================================
File: project.xml       Status: Up-to-date
Working revision:    1.19
Repository revision: 1.19     /code-monkey/confi/stuff.xml,v
Sticky Tag:          (none)
Sticky Date:         (none)
Sticky Options:      (none)

Ensure that the working and repository directory are the same.

Tag the current set of files as one save point. Which means you can later refer all those various versions of files stringed together for one tag.

$ cvs tag CODEBASE_2008_08_30

Once tagged, you are ready to branch the code based on that tag.

$ cvs tag -r CODEBASE_2008_08_30  -b CODEBASE_BRANCH_2008_08_30

This creates a branch of your code now called CODEBASE_BRANCH_2008_08_30

Now you can switch to the branch code using the following command:

$ cvs up -r CODEBASE_BRANCH_2008_08_30

Now you can confirm as well which branch you are on by using the stat command on the same file.

$ cvs stat stuff.xml
===================================================================
File: project.xml       Status: Up-to-date
Working revision:    1.9
Repository revision: 1.9     /code/stuff.xml,v
Sticky Tag:          CODEBASE_BRANCH_2008_08_30 (branch: 1.9.1)
Sticky Date:         (none)
Sticky Options:      (none)


Working on the branch
:
Working isn’t any different from working on the trunck. All the code that you commit while you are on the branch goes straight into the branch.

Merging the branch to the trunk:
The important thing to remember is before merging you need to switch back to trunck.

You can now verify using cvs stat command as explained above.

Doing the actual merge

cvs up -j CODEBASE_BRANCH_2008_08_30

Now examine the output for any conflicts etc and then fixing them. Its worth at this point of time to run the application and check everything is working fine, run your test suites etc. Once confirmed everything is working fine, its time to check stuff back into the cvs.

You can now either do a full commit of your working directory which is not a good idea or identify which files had to be merged and check those only. To get to know the state of the current working directory run the following command:

$ cvs -nq up

and it will show you all the files with their status’. The files starting with M are the once which have been modified and the once with C are conflicts, resolve them as usual and then you can check files marked as M back into cvs with the comment that they have been merged from the branch.

About rp

Architect for large, highly scalable LAMP applications and Technical Manager with special focus on metrics based continuous improvement of teams and products. Rajat has close to a decade of experience of a very wide range of skills related to infrastructure, middleware, app servers all the way to front-end technologies and software development methodologies including agile, iterative waterfall, waterfall as well as ah-hoc startup using the right approach in the right context to reduce time to market.