Codementor Events

How to synchronise between SVN and Git. | by Melisha Trout | CodeX

Published Jan 30, 2023
How to synchronise between SVN and Git. | by Melisha Trout | CodeX

In my last post I mentioned how one could migrate from SVN to Git. In this post I will explain how to synchronise any changes made between Git and Subversion (SVN). There will always be that one person in your team that did not get the memo and committed some changes to SVN whilst you were in the middle of the migration. Don’t stress yourself out about it there is a fix, and I will show you how.
The process is pretty straightforward and will include the following steps:

  • Creating a sync branch
  • Merge changes from SVN to Git
  • Optional: Merge changes from Git to SVN (if needed)

Please Note according to the git svn documentation there is a command called git svn rebase. The git svn rebasecommand retrieves any changes made in the SVN repository and applies the changes to the current work within Git. This command is similar to svn update or git pull however, whenever I have tried to use this command in the past it would timeout on me. That is why I used the following process to sync the changes successfully. However, if this command works for you, please use it, yet if it does not, then continue reading.

Create a sync branch

We will need to create a new branch within git, which we will call svn_sync. This will allow us to pull any changes made from the main branch after the migration. Also, note that this branch will only be used to set up a connection to SVN, it will not be used for any development.

git branch --no-track svn_sync

Once the branch is created, checkout the branch and initialise the repository to the SVN repo. Once initialised, fetch the SVN repo making sure that the authors file that was used in the original migration is up-to-date.

git svn init -s https://mycompany.com/path/to/svn/repo
git svn fetch --authors-file=../authors.txt

If needed you can set the author’s file location so that you don’t have to type it out again.

git config svn-remote.svn.authorsfile ~/authors.txt

Optional: If you had set up git LFS during the migration, you will need to perform the same setup in the new branch. If you need to remember how this is done, checkout the ‘Convert any large files to LFS objects’ section in my original post:

Merge changes from SVN to Git

Checkout the master branch and then merge the svn_sync into master:

git checkout master
git merge svn_sync

Once everything is merged and there are no conflicts, you can now push the merge changes to the remote repository:

git push origin master

Optional: Merge changes from Git to SVN (if needed)

If you run into an issue whereby you would need to merge changes from git back to SVN then have fear! It’s just a simple process and I will help you through it.
First checkout the current release branch or the most up-to-date branch in git(this can be the master branch) and pull in the changes:

git checkout master
git pull

Then you will need to checkout the svn_sync branch and pull the latest changes:

git checkout svn_sync
git svn fetch
git svn rebase

Now you will need to merge the changes from the release or master branch into the svn_sync branch:

git merge --no-ff master

If there are any conflicts, you will need to resolve them at this stage. Once they are resolved you will need to commit the changes locally and then commit the changes to SVN:

git commit -a
git svn commit

And there you have it, you have successfully synced between SVN and Git. I hope that this post was helpful. If you want to read more of my career advice and tech takes, consider following me as well as signing up for my newsletter so that you will never miss my post.

Discover and read more posts from Melisha Trout
get started
post commentsBe the first to share your opinion
Neon Malwer
a year ago

As an example, let’s say you’re building a virtual account manager (https://sharkstack.co/virtual-account-manager ) and you’re using Git for version control. You can synchronize your Git repository with an SVN repository using the “git-svn” tool. This will allow you to keep both repositories in sync and work with them using the Git workflow. For instance, you can use a service like SharkStack to host your virtual account manager and keep track of changes made to the codebase.

Show more replies