Git-Svn–Working With Multiple But Selected SVN Branches

(Starting from a trunk-only checkout)

Who doesn’t like Git? And who doesn’t have to work with SVN?!

It’s a fairly common practice in a project that I’m working on, for several developers to use git-svn to avoid having to deal with Subversion pain; rest of the team is not ready to move on from SVN, so this is the next best thing we, git-lovers, can do.

Since our work is limited to a particular directory in SVN and there are lots of other things in that same SVN Repo, plus we have a branch per release policy, my repo had only the trunk checked out (+ one of the old release branches). The .git/config file looked something like this –

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
    hideDotFiles = dotGitOnly
[svn-remote "svn"]
    url = https://<svnurl>/<ourSpecificDirectory>/trunk
    fetch = :refs/remotes/git-svn
[svn-remote "rb-1013"]
    url = https://<svnurl>/<ourSpecificDirectory>/branches/RB-10.13
    fetch = :refs/remotes/rb-1013

The two remotes, actually work like two separate remotes. The “rb-1013” would download all the data from SVN all over again, without worrying about what “git-svn” already had.

Now I had to checkout another branch – and doing it this way would take hours just to download all the data again. I could use the “-r” flag and start from a specific revision, but that too will still download all the artifacts at least – and I just din’t have so much time. It also din’t make sense to me that we need to download all those revisions when the trunk already has the same revisions as the branch uptil the point of branch creation. So, after quite some research, this is what I did.

  • Backed up my git repo (:D)
  • Removed all the rb related references from .git/config (the section for rb-1013 above – just to clean up, din’t really need that branch any more).
  • Changed the git-svn reference in config file from – >

[svn-remote "svn"]
                url = https://<svnurl>/<ourSpecificDirectory>/trunk
                fetch = :refs/remotes/git-svn

TO

[svn-remote "svn"]
        url = https://<svnurl>/<ourSpecificDirectory>
        fetch = trunk:refs/remotes/git-svn
        branches = branches/{RB-10.26}:refs/remotes/branches/*

Basically this is to ensure that the branches are related to the trunk, so same commits are not downloaded again. The refs/remotes/branches/* is a wildcard that creates remotes for each branch that is present. And the high-lighted portion is a comma-separated filter will restrict the branches we download.

Note that since the overall trunk path is still same, it din’t break the existing revisions in master (which pointed to git-svn).

  • git svn fetch

That took a long while with no response – after some more research, I found the .git/svn/metadata file – opening this showed that the file was indeed getting updated, something called branches-maxRev was getting updated – it started searching from revision 0 to see where the branches were created which are not yet fetched locally.

Note that it was *not* downloading all the revisions, it was just searching svn to see when it created the branch (because we don’t have that info locally).

Not fast-enough though – CTRL-C!!

  • Found the second reference below – basically suggesting if we know the revision on which the SVN branch was created, we can speed up the process. No problem right? Well, went to https://<svnbaseurl>/viewvc and used the browser client to see that the RB-10.26 branch was created on revision 29313
  • Re-run the above command but with this revision

    git svn fetch –r 29313:HEAD

    Ok, some things start downloading! I can see RB-10.26 in the refs path in git-svn logs.

  • After everything downloads, verify using “git branch –r”
  • BOOM!

And it works. Took a few minutes to download the diff revs, but now I have a new remote: /branches/RB-10.26. Now I create a new branch in git to point to this reference

  • git checkout –b production26 remotes/branches/RB-10.26
  • and verify

git svn info

Shows the branch instead of trunk – switching to master shows trunk.

The actual git-svn download took few minutes – I’ve never been able to download an SVN branch *ever* so fast, especially when using SVN, so this is a big win for Git, even when working with SVN!

 

References:

http://stackoverflow.com/a/10173516/297964

http://stackoverflow.com/q/10878976/297964

https://www.kernel.org/pub/software/scm/git/docs/git-svn.html

Leave a Reply

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

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax