Migrating from svn to git in 8 steps preserving the history 您所在的位置:网站首页 git-svn Migrating from svn to git in 8 steps preserving the history

Migrating from svn to git in 8 steps preserving the history

2023-03-30 16:33| 来源: 网络整理| 查看: 265

In this guide we will see how to migrate a svn repository to git, preserving its history.

All the commands listed in this guide can be executed from a Linux , MacOsX or Windows environment, in the latter case I strongly suggest you install git for Windows which includes the git bash (https://git-scm.com/downloads). 

MacOsX users may need to also install git-svn, you can grab it from here: https://formulae.brew.sh/formula/git-svn.

Once the necessary download and install have been completed, let’s get started.

Step 1. Export svn users

Suppose we need to migrate the SVN repository located at:

https://mysvnhost/mypath/mysvnreponame

Run the command:

$ svn log https://mysvnhost/mypath/mysvnreponame --quiet | grep -E "r[0-9]+ \| .+ \|" | cut -d'|' -f2 | sed 's/ //g' | sort | uniq > /home/myuser/svn-authors.txt

Open the file you just created with a text editor and modify the lines as follows:

svn-user = git-user

one line per user (including angle brackets ‘’), e.g.:

j.smith = John Smith

As the last line add:

(no author) = gitsvn

To the right of the equal you can change the username and email as you prefer, this is a default git user that will be used when a commit svn does not contain the author.

Step 2. Initializing the git repository

Create a folder that will contain the git repository:

$ mkdir /home/myuser/mygitrepo $ cd /home/myuser/mygitrepo

and run the command:

$ git svn init https://myhost/mypath/myreponame -stdlayout -no-metadata

Next import users into the git repository you just created:

$ git config svn.authorsfile /home/myuser/svn-authors.txt

Step 3. Import the repo from SVN

Run the command:

$ git svn fetch

Now grab some coffee or tea and wait for the repository to be imported. Depending on its size, it can take from a few minutes to hours. If the process stops for any reason you can re-run the command and the process will pick up where you left off.

At this point git-svn will have imported the main branch and all the branches and tags from the original SVN repository. At this point you can run the command:

$ git branch -a

to see the result. For example:

* master remotes/origin/branch1 remotes/origin/branch2 remotes/origin/branch3 ... remotes/origin/tags/tag1 remotes/origin/tags/tag2 ... remotes/origin/trunk

Depending on the layout of your SVN repository you may find a branch called remotes/origin/trunk or remotes/origin/git-svn.

Delete that branch; type:

$ git branch -d -r origin/git-svn

or

$ git branch -d -r origin/trunk

Similarly, you can remove any branches that you don’t want get imported into the git repository.

Since SVN does not have real support for tags (they are actually branches), they will be imported as branches during fetch. It is therefore necessary to convert them into real git tags. You also need to convert the remote branches to local branches.

Step 4. Conversion of tags

To convert tags, create a shell script called convert-tags.sh in the /home/myuser directory as follows:

for t in `git branch -a | grep 'tags/' | sed s_remotes/origin/tags/__` ; do git tag $t origin/tags/$t git branch -d -r origin/tags/$t done

give it the necessary grants to be executed:

$ chmod u+x ../convert-tags.sh

and finally run it:

$ ../convert-tags.sh

Now verify that the remote tags have been converted to local tags:

$ git –l tag tag1 tag2 ...

Step 5. Converting branches

As with the tags, you now need to convert the remote branches to local branches and delete the remote branches:

To convert branches, create a shell script convert-branches.sh in the /home/ myuser directory as follows:

for t in `git branch -r | sed s_origin/__` ; do git branch $t origin/$t git branch -D -r origin/$t done

give it the necessary grants to be executed:

$ chmod u+x ../convert-branches.sh

and finally run it:

$ ../convert-branches.sh

Now verify that the remote branches have been converted to local branches :

$ git branch –a branch1 branch2 ... * master

Optionally, you can rename the master branch to main as follows:

$ git branch -m master main

Step 6. Cleaning the SVN stuff

$ git config --remove-section svn-remote.svn $ git config --remove-section svn $ rm -fr .git/svn .git/{logs,}/refs/remotes/svn

Step 7. Add the remote site

Here I am assuming that the remote repository is hosted by Github. Of course you can change the URL according to your needs:

$ git remote add origin https://github.com/mypath/myrepo.git

If you haven’t already done at global level, you may want to configure your “local” git user, adding it to the configuration of the local repository. Run the following commands:

$ git config user.name "Your Name" $ git config user.email [email protected]

If the remote Github site has commits, you may need to merge the logs. Run the command:

$ git pull --allow-unrelated-histories origin main

Step 8. Push the repository

Almost done. The last step is to upload the repository to the remote:

$ git push --set-upstream --all origin

Congratulations, you have completed the migration!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有