Migrating a SVN repository to Git (Bitbucket)
This article explains how to migrate a SVN repository to Git. Although this guide uses BitBucket as the Git repository, you can easily adjust the steps to migrate to a different Git repository provider.
I was recently tasked to migrate a repository from SVN to Git (BitBucket). I have tried the the importer from BitBucket but it failed due to corruption in our SVN repository.
So I had no other alternative than to do things by hand. Below is the process I have used and some gotchas.
Authors
SVN stores just a username with every commit. So nikos
could be one and Nikos
could be another user. Git however stores also the email of the user and to make things work perfectly we need to create an authors.txt
file which contains the mapping between the SVN users and the Git users.
NOTE The authors.txt
file is not necessary for the migration. It only helps for the mapping between your current users (in your Git installation).
The format of the file is simple:
captain = Captain America <[email protected]>
If you have the file ready, skip the steps below. Alternatively you can generate the authors.txt
file by running the following command in your SVN project folder:
svn log -q | \
awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | \
sort -u > authors.txt
Conventions
- The source SVN repository is called
SVNSOURCE
- The target GIT repository is called
GITTARGET
- The SVN URL is
https://svn.avengers.org/svn
Commands
Create a work folder and cd
into it
mkdir source_repo
cd source_repo/
Initialize the Git repository and copy the authors file in it
git svn init https://svn.avengers.org/svn/SVNSOURCE/ --stdlayout
cp ../authors.txt .
Set up the authors mapping file in the config
git config svn.authorsfile authors.txt
Check the config just in case
git config --local --list
The output should be something like this:
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
svn-remote.svn.url=https://svn.avengers.org/svn/SVNSOURCE
svn-remote.svn.fetch=trunk:refs/remotes/trunk
svn-remote.svn.branches=branches/*:refs/remotes/*
svn-remote.svn.tags=tags/*:refs/remotes/tags/*
svn.authorsfile=authors.txt
Get the data from SVN (rerun the command if there is a timeout or proxy error)
git svn fetch
Check the status of the repository and the branches
git status
git branch -a
Create the new bare work folder
cd ..
mkdir source_bare
cd source_bare/
Initialize the bare folder and map the trunk
git init --bare .
git symbolic-ref HEAD refs/heads/trunk
Return to the work folder
cd ..
cd source_repo/
Add the bare repo as the remote and push the data to it
git remote add bare ../source_bare/
git config remote.bare.push 'refs/remotes/*:refs/heads/*'
git push bare
Return to the bare work folder and check the branches
cd ..
cd source_bare/
git branch
Rename trunk to master
git branch -m trunk master
Note all the branches that are prefixed /tags/
and modify the lines below (as many times as necessary) to convert SVN tags to Git tags
git tag 3.0.0 refs/heads/tags/3.0.0
...
git branch -D tags/3.0.0
...
Alternatively you can put the following in a script and run it:
git for-each-ref --format='%(refname)' refs/heads/tags | \
cut -d / -f 4 | \
while read ref
do
git tag "$ref" "refs/heads/tags/$ref";
git branch -D "tags/$ref";
done
Check the branches and the new tags
git br
git tags
Check the authors
git log
Push the repository to BitBucket
git push --mirror [email protected]:avengers/GITTARGET
Enjoy
-
Nikolaos Dimopoulos
Boldly goes where no other coder has gone before.... and other ramblings
Recent Posts
-
Setting up Docker for Qubes OS
2024-10-05 -
PhpStorm cannot create scratch files
2023-12-07 -
PHP 8.2 Deprecation of Dynamic Properties
2023-07-18 -
New Look
2023-06-12 -
Linux Swap file in RAM
2023-04-17
Tag Cloud
-
amazon (3)
android (1)
angularjs (7)
apps (1)
aurora (1)
aws (1)
backup (2)
bash (1)
bitbucket (1)
blog (2)
books (1)
bootstrap (1)
buzz (1)
cPanel (1)
cache (1)
celebrations (4)
chromium (3)
chromium os (3)
cloud computing (3)
codacy (1)
codecov (1)
communications (1)
composer (1)
conversion (1)
copy (1)
degoogle (5)
design (1)
design patterns (3)
discord (1)
docker (1)
docs (3)
documentation (1)
ec2 (3)
emerge (1)
encoding (1)
factory (1)
froyo (1)
fujitsu (1)
gentoo (7)
git (3)
github (2)
gmail (3)
google (16)
google apps (4)
google maps (1)
gource (1)
ha (1)
hosting (2)
how to (36)
igbinary (1)
information (5)
input (1)
installation (6)
internet (1)
iphone (1)
json (2)
libreoffice (1)
linux (13)
localization (1)
lts (1)
mariadb (1)
memorial day (1)
metrics (1)
migration (1)
mod_rewrite (1)
mov (1)
mp4 (1)
mysql (6)
nas (1)
netlify (1)
new look (1)
nexus one (2)
nfs (1)
notebook (1)
online storage (1)
openoffice (1)
opinion (1)
oracle (1)
patterns (1)
payroll (1)
performance (3)
personal (9)
phalcon (12)
php (23)
php8 (2)
php82 (1)
phpstorm (1)
phpunit (2)
picasa (2)
portage (1)
privacy (1)
programming (9)
proxy (1)
qubes os (1)
rant (5)
rdbms (1)
rds (1)
relationships (1)
release (1)
remove (1)
replication (1)
review (9)
rsync (2)
s1300 (1)
scan (1)
scratch (1)
serialize (1)
series (9)
singleton (1)
sorting (1)
spaceship (1)
spam (1)
ssl (1)
static (1)
storage (6)
submodules (1)
subversion (2)
svn (1)
swap (1)
tdd (1)
technorati (1)
test driven development (1)
testability (1)
testing (2)
titles (1)
traits (1)
ua (1)
ubuntu (1)
update (6)
upgrade (1)
usa (2)
usort (1)
utf8 (1)
video (1)
visualization (1)
vps (1)
webm (1)
website (1)
wget (1)
zend framework (4)
zram (1)
zstd (1)