How do I create a branch?
Asked Answered
H

10

671

How do I create a branch in SVN?

Hedveh answered 5/8, 2008 at 9:25 Comment(0)
M
377

Branching in Subversion is facilitated by a very very light and efficient copying facility.

Branching and tagging are effectively the same. Just copy a whole folder in the repository to somewhere else in the repository using the svn copy command.

Basically this means that it is by convention what copying a folder means - whether it be a backup, tag, branch or whatever. Depending upon how you want to think about things (normally depending upon which SCM tool you have used in the past) you need to set up a folder structure within your repository to support your style.

Common styles are to have a bunch of folders at the top of your repository called tags, branches, trunk, etc. - that allows you to copy your whole trunk (or sub-sets) into the tags and/or branches folders. If you have more than one project you might want to replicate this kind of structure under each project:

It can take a while to get used to the concept - but it works - just make sure you (and your team) are clear on the conventions that you are going to use. It is also a good idea to have a good naming convention - something that tells you why the branch/tag was made and whether it is still appropriate - consider ways of archiving branches that are obsolete.

Maleate answered 5/8, 2008 at 13:40 Comment(3)
"svn copy" has the advantage that it will retain history previous to the branching. Manually copying to another directory won't.Goldston
Also note that it's usually a bad idea to tag or branch subdirectories of "trunk". This makes it difficult to keep track of which subdirectory was branched, and most tools will get confused by these branches (e.g. switching branches will mean the directory structure of the WC changes, which will confuse IDEs and build tools). Just always branch "trunk".Holocaine
@Will Actually svn cp uses cheap copies, it does not copy the actual files during the branching. See svnbook.red-bean.com/en/1.1/ch04s02.htmlMacrophage
H
647

Create a new branch using the svn copy command as follows:

$ svn copy svn+ssh://host.example.com/repos/project/trunk \
           svn+ssh://host.example.com/repos/project/branches/NAME_OF_BRANCH \
      -m "Creating a branch of project"
Hedveh answered 5/8, 2008 at 9:25 Comment(6)
Next use svn switch svn+ssh://host.example.com/repos/project/branches/NAME_OF_BRANCH . (if you want to switch current checkout to new branch) or svn checkout svn+ssh://host.example.com/repos/project/branches/NAME_OF_BRANCH (if you want to have new branch in seperate directory) to start working on newly created branch.Meade
Note: in some cases you would need to use --parents option with svn copy!Meade
for some reason I used this cmd and it didn;t work, but when i changed the svn+ssh to just https it did work. Did I do anything wrong? what is the meaning of svn+ssh? Thanks!Shulins
you have to create first a new directory in your branch -> svn mkdir host.example.com/repos/project/branches/NAME_OF_BRANCH -m "make the branches directory to hold all the branches"Slovak
If I omit the -m option, it says like svn: E155010: Path '/home/constantine/someDirectory/svn+ssh:https:/myhost.com/svn/dir1/dir2/trunk/dir3/dir4/dir5' does not exist. With -m it says svn: E205009: Local, non-commit operations do not take a log message or revision properties. (I replaced the addresses, but they do exist, except, ofc, the new branch directory, and the whole path+url that SVN for some reason merged in the error).Gwendagwendolen
Okay, the answer below worked. Since, I guess, svn cp == svn copy, the part svn+ssh in this answer is wrong, it doesn't work (triggers the above error ↑ ).Gwendagwendolen
M
377

Branching in Subversion is facilitated by a very very light and efficient copying facility.

Branching and tagging are effectively the same. Just copy a whole folder in the repository to somewhere else in the repository using the svn copy command.

Basically this means that it is by convention what copying a folder means - whether it be a backup, tag, branch or whatever. Depending upon how you want to think about things (normally depending upon which SCM tool you have used in the past) you need to set up a folder structure within your repository to support your style.

Common styles are to have a bunch of folders at the top of your repository called tags, branches, trunk, etc. - that allows you to copy your whole trunk (or sub-sets) into the tags and/or branches folders. If you have more than one project you might want to replicate this kind of structure under each project:

It can take a while to get used to the concept - but it works - just make sure you (and your team) are clear on the conventions that you are going to use. It is also a good idea to have a good naming convention - something that tells you why the branch/tag was made and whether it is still appropriate - consider ways of archiving branches that are obsolete.

Maleate answered 5/8, 2008 at 13:40 Comment(3)
"svn copy" has the advantage that it will retain history previous to the branching. Manually copying to another directory won't.Goldston
Also note that it's usually a bad idea to tag or branch subdirectories of "trunk". This makes it difficult to keep track of which subdirectory was branched, and most tools will get confused by these branches (e.g. switching branches will mean the directory structure of the WC changes, which will confuse IDEs and build tools). Just always branch "trunk".Holocaine
@Will Actually svn cp uses cheap copies, it does not copy the actual files during the branching. See svnbook.red-bean.com/en/1.1/ch04s02.htmlMacrophage
J
43

If you're repo is available via https, you can use this command to branch ...

svn copy https://host.example.com/repos/project/trunk \
       https://host.example.com/repos/project/branches/branch-name \
  -m "Creating a branch of project"
Joanjoana answered 10/2, 2016 at 20:36 Comment(0)
Z
28
svn cp /trunk/ /branch/NEW_Branch

If you have some local changes in trunk then use Rsync to sync changes

rsync -r -v -p --exclude ".svn" /trunk/ /branch/NEW_Branch
Zygophyte answered 3/9, 2013 at 9:14 Comment(5)
There is no requirement to use rsync in this way. svn cp will also copy any local changes.Recently
@KevinPanko It won't copy uncommitted changes.Zygophyte
It does, confirmed with svn, version 1.8.5Recently
I am wondering about this. This seems like the easiest option, and AFAI remember, this is what I did when I was working with SVN 3 years ago. But what is the difference between this and the ones working directly with the server urls?Behka
@KevinPanko Do you know whether it also copies newly created files?Parados
T
18

Top tip for new SVN users; this may help a little with getting the correct URLs quickly.

Run svn info to display useful information about the current checked-out branch.

The URL should (if you run svn in the root folder) give you the URL you need to copy from.

Also to switch to the newly created branch, use the svn switch command:

svn switch http://my.repo.url/myrepo/branches/newBranchName
Transmogrify answered 28/3, 2014 at 11:30 Comment(1)
You can also use ^ in place of the Repository RootTransmogrify
O
17

Suppose you want to create a branch from a trunk name (as "TEST") then use:

svn cp -m "CREATE BRANCH TEST" $svn_url/trunk $svn_url/branches/TEST
Oberstone answered 23/7, 2013 at 9:51 Comment(0)
J
15

Normally you'd copy it to svn+ssh://host.example.com/repos/project/branches/mybranch so that you can keep several branches in the repository, but your syntax is valid.

Here's some advice on how to set up your repository layout.

Jevons answered 5/8, 2008 at 12:40 Comment(0)
O
12

If you even plan on merging your branch, I highly suggest you look at this:

Svnmerge.py

I hear Subversion 1.5 builds more of the merge tracking in, I have no experience with that. My project is on 1.4.x and svnmerge.py is a life saver!

Ossieossietzky answered 5/8, 2008 at 12:46 Comment(0)
E
6
  • Create a new folder outside of your current project. You can give it any name. (Example: You have a checkout for a project named "Customization". And it has many projects, like "Project1", "Project2"....And you want to create a branch of "Project1". So first open the "Customization", right click and create a new folder and give it a name, "Project1Branch").
  • Right click on "Myproject1"....TortoiseSVN -> Branch/Tag.
  • Choose working copy.
  • Open browser....Just right of parallel on "To URL".
  • Select customization.....right click then Add Folder. and go through the folder which you have created. Here it is "Project1Branch". Now clik the OK button to add.
  • Take checkout of this new banch.
  • Again go to your project which branch you want to create. Right click TorotoiseSVN -> branch/tag. Then select working copy. And you can give the URL as your branch name. like {your IP address/svn/AAAA/Customization/Project1Branch}. And you can set the name in the URL so it will create the folder with this name only. Like {Your IP address/svn/AAAA/Customization/Project1Branch/MyProject1Branch}.
  • Press the OK button. Now you can see the logs in ...your working copy will be stored in your branch.
  • Now you can take a check out...and let you enjoy your work. :)
Edsel answered 5/2, 2014 at 10:24 Comment(0)
A
5

Below are the steps to create a branch from trunk using TortoiseSVN in windows machine. This obviously needs TortoiseSVN client to be installed.

  1. Right Click on updated trunk from local windows machine
  2. Select TortoiseSVN
  3. Click branch/Tag
  4. Select the To path in SVN repository. Note that destination URL is updated according to the path and branch name given
  5. Do not create folder inside branches in repository browser
  6. Add branches path. For example, branches/
  7. Add a meaningful log message for your reference
  8. Click Ok, this creates new folder on local system
  9. Checkout the branch created into new folder
Aggy answered 27/3, 2020 at 5:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.