Branching, Merging and Restoring branches with SVN

The modern day teams use modern version control system like Git and Mercurial and no question they are better in usability than the version control systems that were popular like 5 years ago something like an SVN. But if you are still using or if you have to use SVN and need to use features like branching this blog might prove to be of some help. Don’t panic it is not as bad as Git or other modern VCS’s hype it to be IMO. Below I am listing just a couple ways that you could use branching. In this article, I will be covering:

(a) Creating a new branch
(b) Merging from trunk to your branch
(c) Merging your branch to the trunk
(d) Deleting a branch
(e) Restoring a deleted branch

Creating a new SVN Branch

If you or your team are working on a new module and you would like to keep your trunk production you will go about create a new branch to work on. The process of creating a new branch and this assuming you would diverge from the trunk:

  • Checkout the complete svn project if you haven’t already.

    svn copy svn+ssh://host.example.com/sources/project

    If you have an existing complete project then cd into your trunk and “svn update” it.

  • Copy the latest trunk into the branch
    svn copy svn+ssh://host.example.com/sources/project/trunk 
    svn+ssh://host.example.com/sources/project/branches
    /YOUR_NEW_CREATED_BRANCH 
    -m "Creating a branch for "

    The output of the above command would be similar to your The above command and done your new branch is created.

  • If your team mate has checked out trunk lets say and would like to switch their branch:
    svn switch svn+ssh://host.example.com/sources/project
    /branches/YOUR_NEW_CREATED_BRANCH

    to make sure a branch is checked out use:

    svn info

Merging from trunk to your branch

svn merge svn+ssh://host.example.com/sources/project/trunk .

use the ‘–dry-run’ option to check the incoming changes without actually making the merge; use ‘-r:’ to merge revisions.

svn commit -m "Your merge commit message"

Merging your branch to the trunk

svn merge svn+ssh://host.example.com/sources/project/trunk  
svn+ssh://host.example.com/sources/project/branches
/YOUR_NEW_CREATED_BRANCH

Again you could use the the flags mentioned above.

svn commit -m "Your merge commit message from branch to trunk"

Deleting a branch

Once you are done merging your branch -> trunk and cd into the project root dir

svn delete 
svn+ssh://host.example.com/sources/project/branches
/YOUR_NEW_CREATED_BRANCH   
-m "commit message"

Restoring a deleted branch

In case a branch needs to be restored and you need to lookup something.

svn copy 
svn+ssh://host.example.com/sources/project/branches
/YOUR_NEW_CREATED_BRANCH@rev_num
svn+ssh://host.example.com/sources/project/branches
/YOUR_NEW_CREATED_BRANCH_restored
-m "commit message for restoration"

Small Tips

  1. Lets say you have been merging and diverging away from trunk frequently and when you are trying to merge trunk in your branch and you see a tree conflicts. What I would do is backup the file text conflicts: 
    svn resolve --accept working

    Then you might want to update the files appropriately and commit the code.

  2. When you work on merging, merge as frequently as you can. I would check for updates to the trunk every 2-3 days and bring the incoming from trunk to your branch.
  3. If you are working in a team. I would merge when all the team members are present.

I hope this has saved time for people reading this blog. I have some SVN knowledge under my belt and would be happy to answer the questions to the best of my knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *

13 − two =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>