Step By Step Subversion Branching And Merging
Subversion is being phased out in favor of git so if you are just starting a project I recommend you hop on the git bandwagon right away. If you are stuck on svn though and need to branch and merge here’s a quick how-to.
Branching
svn cp https://myserver.dev/project/project_name/trunk https://myserver.dev/project/project_name/branches/branch_name
cd
svn co https://myserver.dev/project/projectname/branches/branch_name
cd branch_name
Now you can do your work in the branch as you would in trunk.
Merging To The Branch
Most people like to merge from trunk to the branch to make sure that tests still run. That way if you have to do a few various fixes you can do them step by step, each with their own checkin.
In order to do so in SVN you have to know what revision you merged at. This is tedious but there is a trick that the piston creator showed me.
Run this from the folder you have your branch checked out in.
svn log --stop-on-copy https://myserver.dev/project/projectname/branches/branch_name --revision HEAD:1
Logs will appear and you just have to remember the last revision number that appears. It is preceded by a lower case r. In my case it was r555 so 555 is the important number for me.
Now still in the same branch folder take that revision number and add it to this command:
svn merge -r 555:HEAD https://myserver.dev/project/project_name/trunk
Make sure that your tests run and if you have any conflict you have to manually edit the files to resolve them. Once everything is fine you can commit your changes to the branch.
svn commit -m "merged from trunk to branch"
Merging Back To Trunk
Now you have to go back to the folder where you edit your trunk. Here you must use the revision number you used earlier. The one that determines when you branched.
svn merge -r 555:HEAD https://myserver.dev/project/projectname/branches/branch_name
Hopefully you don’t have any conflicts but if you do open each conflicting files. Edit them to your liking and then use svn resolved filename. Finish up with a commit.
So that’s it. The good news for those of you stuck with SVN their next version will make this process slightly easier. Of course I hope that before they release the new version that you’ve joined the distributed wagon.
on June 9th, 2008 at 11:26 am
Good writeup, Gary!
on June 9th, 2008 at 11:28 am
@francois: thank in great part to what you thought me on merging.
on June 9th, 2008 at 2:00 pm
Nice writeup!
I hope I never have to through that again.
on June 9th, 2008 at 3:55 pm
You can also use svnmerge if you’re too lazy to remember all those numbers
svn cp …
svn co …
svnmerge.py init
svn ci
then each time you wanna sync w/ trunk:
svnmerge.py merge