Skip to content

Instantly share code, notes, and snippets.

@cbmeeks
Last active March 5, 2025 19:00
Show Gist options
  • Save cbmeeks/f4e65f0435d91312d79447176e5812ba to your computer and use it in GitHub Desktop.
Save cbmeeks/f4e65f0435d91312d79447176e5812ba to your computer and use it in GitHub Desktop.
Instructions on how to convert SVN repos into git (Bitbucket)

How to Import SVN Repos into BitBucket

Create New Repopository in Bitbucket

NOTE, if you create an empty repo in Bitbucket, it will default to the "main" branch and create a .gitignore file. Normally, this would be OK. However, the .gitignore file will cause a conflict because the newly created repo will not be empty. You will need to pull then push.

It is recommended to NOT create a .gitignore file by default and make sure the new repo is completely empty.

Prepare an Authors Mapping File (Optional but Recommended)

SVN stores only usernames for commits, while Git requires a name and email address for each author. To map SVN usernames to Git-compatible author details, create an authors.txt file:

svnuser1 = FirstName LastName <[email protected]>
svnuser2 = Another Name <[email protected]>

You can generate a list of SVN authors by running:

svn log <SVN_REPO_URL> --quiet | grep -E "r[0-9]+ \| .+ \|" | cut -d'|' -f2 | sed 's/ //g' | sort | uniq > authors.txt

Edit the file manually to add names and email addresses. If you skip this step, git-svn will use the SVN username as both the name and email, which may not be ideal.

Save this file (e.g., ~/authors.txt).

Clone the SVN Repository to a Local Git Repository

Use git-svn to clone your SVN repository, converting its history into Git commits. Make sure you are in the parent folder of your repo as the following command will create a new directory from whatever you put as <LOCAL_DIR>. Run the following command:

git svn clone <SVN_REPO_URL> --stdlayout <LOCAL_DIR>
  • <SVN_REPO_URL>: The URL of your SVN repository (e.g., http://svn.example.com/repo).
  • --stdlayout: Assumes your SVN repo uses the standard trunk, branches, and tags structure. If it doesn’t, see the note below.
  • --authors-file=~/authors.txt: Path to your authors mapping file (optional).
  • <LOCAL_DIR>: The directory where the new Git repository will be created (e.g., my-git-repo).

For example:

cd ~/Projects
git svn clone http://svn.example.com/repo --stdlayout my-git-repo

You should now have a folder called "my-git-repo" that contains the repo.

This process may take time depending on the size of your repository and the number of revisions.

If your repo doesn't follow standard svn layout

If your repo doesn't have the normal "trunk", "branch", "tag" folder structure, you will have to clone them a different way. Start by finding the folder structure:

svn ls <SVN_REPO_URL>

You should get a list of folders/files. If no "trunk" is given, then "/" is your trunk.

Run git svn clone with custom paths

git svn clone <SVN_REPO_URL> --trunk=<TRUNK_PATH> --branches=<BRANCHES_PATH> --tags=<TAGS_PATH> <LOCAL_DIR>

If you have no branches or tags folders then ignore those.

Example (assuming "project" is a folder):

git svn clone http://svn.example.com/repo --trunk=project my-git-repo

Navigate to the New Git Repository

Once the cloning is complete, move into the new Git repository:

cd my-git-repo

Clean Up Branches and Tags

The git-svn tool imports SVN branches and tags as remote branches (e.g., remotes/origin/trunk, remotes/origin/tags/tag1). You’ll need to convert these into proper Git branches and tags.

Rename trunk to master (or main): If your SVN trunk was imported as a branch, rename it to align with Git conventions:

git branch -m trunk master

Convert SVN Tags to Git Tags

SVN tags are imported as branches under remotes/origin/tags/. Convert them to proper Git tags with this script:

git for-each-ref --format='%(refname)' refs/remotes/origin/tags | while read ref; do
    tagname=$(echo $ref | cut -d'/' -f5)
    git tag "$tagname" "$ref"
    git branch -r -d "origin/tags/$tagname"
done

This creates Git tags and removes the redundant tag branches.

Convert SVN Branches to Git Branches

Move other SVN branches (e.g., remotes/origin/branch1) to local Git branches:

git branch branch1 refs/remotes/origin/branch1
git branch -r -d origin/branch1

Repeat for each branch you want to keep.

Add Bitbucket as a Remote

Link your local Git repository to the empty Bitbucket repository

git remote add origin <BITBUCKET_REPO_URL>
  • <BITBUCKET_REPO_URL>: The URL of your Bitbucket repository (e.g., [email protected]:username/repo.git).
  • You can find this on the Bitbucket repository page under "Clone."

Push the Repository to Bitbucket

Push all branches and tags to Bitbucket:

git push origin --all
git push origin --tags
  • --all: Pushes all branches.
  • --tags: Pushes all tags.

Verify the Migration

Visit your Bitbucket repository in a browser to confirm that the commits, branches, and tags have been successfully migrated. Use git log locally to double-check the history:

git log

Additional Tips

  • Large Repositories: For very large SVN repositories, the git svn clone process can be slow. You can speed it up by specifying a revision range with -r (e.g., -r 1000:HEAD to start from revision 1000).

  • Authentication: If your SVN repository requires credentials, add them to the git svn clone command (e.g., --username=youruser --password=yourpass), or let it prompt you.

  • Testing: Before pushing to Bitbucket, test the local Git repository to ensure everything looks correct.

  • Alternative Tools: If git-svn doesn’t meet your needs, consider tools like svn2git or SubGit, which offer additional features but may require more setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment