git + svn (both remote) in case you need it
Tags: niche, git, svn
Published: 2020-09-21 15:19:53

You may be traveling to work right now and reading this article wondering, is this worth to read? Well, for the majority of you, the answer is no. But in case you are in a situation, where your source code has to be stored in the svn (for example because of some legacy deployment process), but you, and your team, want to start using git and take advantage of pull requests and code reviews, then this article might be for you.

## Overview

The goal is to use git for everyday commits and update svn just before deployment (or when you need it). I found a lot of tutorials, on the internet, for running git and svn side by side, but they concentrate, on how to use git locally and still commit to remote svn. That means, you cannot use git with your team and git is just your own personal helper in this case. However, I want to be able to have remote git and remote svn and synchronize them.

In the end we will have 2 workflows.

* **Feature flow** - this is a process for day-to-day programming. You can create new branches from master branch (to fix a bug or develop new feature) and in the end, you can merge changes back to the master branch. This workflow can be separated from svn, therefore it will be done solely in git.
* **Synchronization flow** - This flow is meant to synchronize changes between git and svn. It's quite easy to put this process into words, but it was much harder to actually make it work. In a nutshell, take all changes from svn and take all changes from git master branch. Mix them together and update them both (git master branch and svn) with the current source code. Easy, right?

## Setup

I will assume, that you have a git remote repository and svn remote repository up and running.

Let's start by cloning git repository `git clone https:\\www.yourgit.com\path\to.git`. Now open .git/config file and add these lines.

```
[svn-remote "svn"]
	url = https://www.yoursvn.com/url
	fetch = you_can_fetch_some_subdirectory_from_svn
```

This will create new remote named svn and git will know, that this is a svn remote. You can find full documentation for [git-svn](https://git-scm.com/docs/git-svn). That's it. You are all set up now, so let's take a look at synchronization process.

### Synchronization

You and your teammates will develop new features and fix bugs with git branches. You may create pull requests and merge all changes to the master branch. But there will come a time to synchronize all git changes with svn. One person on your team, should be able to do this job. These are the steps, that I follow when synchronizing changes with svn.

* Fetch all changes from svn repository and move them to our release branch. We will use `bridge` branch to fetch svn changes. Bridge branch should be local, do not push it to git remote.

```
checkout bridge
git svn rebase
git checkout -b release/<id>
```

* merge all changes from master to this release branch

```
git merge master
```

> If you encounter a conflict while merging, that means somebody changed something in svn and also in git and git cannot solve the issue. Resolve conflicts the same way as you would in standard git merge conflict scenario and then run `git add -A` and `git commit -m "merge master"`.

* update svn by committing all changes that are in release branch. That includes all changes from git.

```
git svn dcommit
```

> Now is the right time to push release branch to git remote, but this step is optional `git push origin release/<id>`

* update git master branch by merging all changes from release branch. That includes all changes from svn.

```
git checkout master
git merge release/<id>
git push origin master
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mpzkuivggrb8oie3409r.png)

As you can see, this sequence of steps can solve also special case, when someone still uses svn and commits changes to svn repository, but some people use git. That's achieved, when we take all changes from svn and move them to release branch.