Igor's Blog

I've written previously about how to do a one time non-standard SVN repository migration to Git which works fine if you want to move your entire SVN repo, but what if you want to move just a subfolder from SVN to Git? This is the guide for you.

Moving a single subfolder is significantly more work than moving the entire repository. Overall the steps are:
  1. Create author mapping file
  2. Clone SVN into a temporary Git repository
  3. Convert svn:ignore properties to .gitignore file
  4. Rewrite the Git index file to update subfolder paths (this is required to preserve file history)
  5. Add a remote origin in the Git repository that files are being merged into
  6. Fetch and merge the remote Git repository files from the temporary Git repository


So lets see this step-by-step. I'm going to assume the following:
 Assumptions
SVN subfolder URL: http://localhost/repos/mysvn/tags/mytag/SubFolder
Working directory: ~/temp
Git target directory: ~/mygit


Create the author mapping file. This will generate a file called authors-transform.txt which contains SVN authors with their Git author equivalents. The default value for the Git author is set to 'User <user@email>' so adjust that if you care about having real author information.
 Commands
cd ~/temp
svn log --quiet http://localhost/repos/mysvn/tags/mytag/SubFolder | grep "^r" | awk '{print $3" = User <user@email>"}' | sort | uniq > authors-transform.txt


Clone the SVN repository into a temporary Git repository and fix up remote references. Note that I set the trunk to the current directory since we are only cloning a single folder from SVN.
 Commands
git svn clone http://localhost/repos/mysvn/tags/mytag/SubFolder --trunk=. -A authors-transform.txt svngit
cd svngit
sed -i .bak -e "s|refs/remotes/origin/trunk|refs/remotes/git-svn|g" .git/config


Transform svn:ignore properties to the .gitignore file. Note this is put into a new directory that has the same name as the subfolder that was cloned.
 Commands
mkdir SubFolder
git svn show-ignore > SubFolder/.gitignore
git add SubFolder/.gitignore
git commit -m 'Convert svn:ignore properties to .gitignore.'




Rewrite the Git index so that the SubFolder appears in the correct location while preserving file history. This essentially moves all files that were cloned from SVN into a new directory called SubFolder and fixes Git history to point to the right files there. I know the documentation provides a sed example of this, however I was doing it on OS X so had to use awk instead.
 Commands
echo '{print $1"\tSubFolder/"$2}' > /tmp/prog.awk
git filter-branch --index-filter \
'git ls-files -s | awk -F"\t" -f /tmp/prog.awk | \
GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
git update-index --index-info && \
if [ -f "$GIT_INDEX_FILE.new" ]; then mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"; fi' \
HEAD
rm /tmp/prog.awk


In the Git directory add a remote to the temporary Git directory we just created, fetch and merge the files, then remove the remote reference.
 Commands
cd ~/mygit
git remote add SubFolder ~/temp/svngit
git fetch SubFolder
git merge SubFolder/master
git remote remove SubFolder


Now you can finally push the change to the target Git repository.
 Commands
git push


The temporary Git repository can be removed at this point.

-i

, , , ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
StrFunc() Online
300 George St Blogumentary
Guru JSON-RPC Tester
Extrudifier Object Designer

Recent Blog Posts

Where to find the last item (Robbie's lost lyrics) in GravityFalls Lake level

Detecting print requests to print contents of a DIV with JavaScript

Using Google DFP with AdSense on responsive pages

How to change the email subject line in Gmail

Force PHP GD library to load JPEG images that have invalid data

How to uninstall Samsung Smart Switch from your Mac

Facebook adds weather forecasts but forgets to adjust them to your time zone

No sound or microphone when answering calls on an iPhone

Replacing the standard Odroid XU4 fan and heatsink

PHP PDO doesn't work binding multiple IN statement values

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again