Thursday, January 10, 2013

Using git-svn for projects with many branches

About branches: Git-svn would normally clone all revisions from all branches by default. This is a time-consuming process, so I strongly recommend adding only those branches, that you need for your work.
We don't specify a branches section in our git configuration, instead we are attaching branches as new remotes. That also means, that we can't create or delete branches on a svn server, they have to be created with the corresponding svn commands.
svn cp /trunk /branches/
svn rm /branches/
1) Cloning the repository, checking out trunk

First create a directory where you want your git repository to be on your harddrive, than initialize a new git repository.
mkdir gitsvn
cd gitsvn
git init
Next you have to edit your git config and add the trunk as svn-remote.

edit .git/config and add:
[svn-remote "svn"]
        url = http:///svn/trunk
        fetch = :refs/remotes/git-svn
Next you clone the repository. This will check all SVN revisions and start a download for every revision of every file of the trunk, so this will take a while.
git svn fetch
You end up with a local branch "master" that tracks the svn remote "trunk". Type "git svn info" to show information.
git svn info
2) Keeping things up to date

You should never work on the master branch, that one is only reflecting the trunk, it's a mirror, so please never change files here. Just update the master to reflect the trunk, that's all.
git svn fetch
git svn merge trunk –ff
The --ff flag forces a fast-forward.

3) Creating new local branches and rebase

Sometimes, you just want to create a new local branch and work on it. (Perhabs a small bugfix branch, that immediatly gets merged into trunk.)
git checkout -b bugfixbranch trunk
... do some work …
git commit -m "nice explaining commit message"
All unit tests are good, and you want to push that branch perhabs directory to svn trunk. But first, we need to rebase. Someone else could have made commits to trunk in the meantime, so you have to apply his changes first, than yours, that means rewriting your local history.
git svn fetch
git rebase -i trunk
.. now you have the choice, you can pick, reword, edit, squash and fix commits …
git svn dcommit –dry-run
.. it's always recommended to make a dry-run first, so you don't push to the wrong location accidentialy
git svn dcommit
4) Adding a new svn remote branch

Because we have so many branches, that git would force you to download them all, we just specify the explicit branches we want to track.
git config --add svn-remote.branchname.url http:///svn/branchname
git config --add svn-remote.branchname.fetch :refs/remotes/branchname
git svn fetch branchname [-r]
git checkout -b localbranchname -t branchname
git svn rebase branchname
I recommend that you specify the revision number here, otherwise git will download every revision, starting with 0 first. If your branch was created at revision 100000, you will have to wait a long time.
git checkout -b localbranchname branchname
git svn info
5) Reintegrating a single commit into trunk

In the most easiest case, you have a branch with just one single commit, so you can just cherry-pick it.
git log -2
Output:
commit 2db1182ddc0e85b3a9c468bd7610a58c3bb99d0a
Author: sascha.prolic 
Date:  Tue Jan 8 11:37:29 2013 +0000
    Nice explaining commit message

git-svn-id: http:///svn/branches/branchname@100000 d12c8f96-4347-4129-8f7d-0f3f6588512b

commit c56d7da69e96a662aa9aad8cd394a381bad64807
Author: sascha.prolic 
Date:  Tue Jan 8 16:08:26 2013 +0000
    created branch

git-svn-id: http:///svn/branches/branchname@100000 d12c8f96-4347-4129-8f7d-0f3f6588512b
As you can see, the first commit is "c56d7da69e96a662aa9aad8cd394a381bad64807", but that one is just the svn copy, we don't want to merge this, so only "2db1182ddc0e85b3a9c468bd7610a58c3bb99d0a" is interessting. Lets change to master, update, run the unit tests and commit.
git checkout master
git svn fetch
git merge trunk –ff
git cherry-pick 2db1182ddc0e85b3a9c468bd7610a58c3bb99d0a
... run unit tests
git svn dcommit


6) Reintegrate a branch into trunk
git checkout -b mergebranch branchname
git svn fetch
git rebase -i trunk
Your editor should pop up with something like this in it:
pick 0c70bbe created new branch
pick a5bf234 branchname foo
pick d24360d branchname bar
Note, that we don't want to merge "0c70bbe", because that would was the svn cp command. Just delete this line. Save the file, and exit. You can even squash commit or edit commit messages. Run the unit tests again.
git checkout master
git merge trunk –ff
git merge mergebranch
git svn dcommit
7) Delete the branch checkout

The following command sequence will delete (locally only) the SVN branch and its history:
git branch -D local-branch
git branch -D -r branch
rm -rf .git/svn/branch
git gc
To remove the branch on the svn server, remove it with svn rm <branchurl>.

8) Converting a local branch into a remote branch

Basically all it amounts to is a rebase, with some svn trickery.

svn cp /trunk /branches/final-remote-name
Next you add that branch to your git config.

git checkout local-feature-branch   # This is the local branch where your prototype feature is committed.
git checkout -b final-local-name  # This makes the local branch derived from your local feature branch that will eventually become the branch that tracks the remote svn branch.
git svn info  # This will say you are following trunk.
git rebase remotes/final-remote-name
Verify
git log
git svn info