Skip to content

Instantly share code, notes, and snippets.

@Gimly Gimly/Git-Import-SVN.ps1
Last active Aug 26, 2019

Embed
What would you like to do?
Import a SVN repository into a Git repository, complete with branches and tags. Script inspired by the method described by StackOverflow answer http://stackoverflow.com/a/3972103/123597
Param(
[Parameter(Mandatory=$true, Position=1)]
[string]$SvnFolderPath,
[Parameter(Mandatory=$true, Position=2)]
[string]$TargetFolder,
[Parameter(Mandatory=$true, Position=3)]
[string]$GitUrl
)
git svn clone --stdlayout --no-metadata -A users.txt $SvnFolderPath "$TargetFolder-tmp"
cd "$TargetFolder-tmp"
$remoteBranches = git branch -r
foreach($remoteBranch in $remoteBranches)
{
$remoteBranch = $remoteBranch.Trim()
if($remoteBranch.StartsWith("tags/"))
{
$tagName = $remoteBranch.Substring(5)
git checkout -b "tag-$tagName" $remoteBranch
git checkout master
git tag $tagName "tag-$tagName"
git branch -D "tag-$tagName"
}
elseif($remoteBranch -notlike "trunk")
{
git checkout -b $remoteBranch $remoteBranch
}
}
cd ..
git clone "$TargetFolder-tmp" $TargetFolder
rm -Recurse -Force "$TargetFolder-tmp"
cd $TargetFolder
$remoteBranches = git branch -r
foreach($remoteBranch in $remoteBranches)
{
$remoteBranch = $remoteBranch.Trim()
if($remoteBranch -notcontains "HEAD" -and $remoteBranch -notcontains "master")
{
$branchName = $remoteBranch.Substring(7)
git checkout -b $branchName $remoteBranch
}
}
git checkout master
git remote rm origin
git remote add origin $GitUrl
git push --all
@JamesSkemp

This comment has been minimized.

Copy link

commented Sep 5, 2017

This is way easier than https://john.albin.net/git/convert-subversion-to-git, which is what I was trying to follow before.

I did have to remove --stdlayout from line 10 to deal with Subversion repos that weren't setup with the standard trunk/branches/tags structure. I was otherwise getting an error about the Git repository being empty.

@tvgm2

This comment has been minimized.

Copy link

commented Aug 26, 2019

This is way easier than https://john.albin.net/git/convert-subversion-to-git, which is what I was trying to follow before.

I did have to remove --stdlayout from line 10 to deal with Subversion repos that weren't setup with the standard trunk/branches/tags structure. I was otherwise getting an error about the Git repository being empty.

Thanks for the tip @JamesSkemp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.