Migrating from svn to git in 8 steps preserving the history | 您所在的位置:网站首页 › git-svn › Migrating from svn to git in 8 steps preserving the history |
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.txtOpen 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 SmithAs the last line add: (no author) = gitsvnTo 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/mygitrepoand run the command: $ git svn init https://myhost/mypath/myreponame -stdlayout -no-metadataNext import users into the git repository you just created: $ git config svn.authorsfile /home/myuser/svn-authors.txtStep 3. Import the repo from SVN Run the command: $ git svn fetchNow 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 -ato 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/trunkDepending 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-svnor $ git branch -d -r origin/trunkSimilarly, 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 donegive it the necessary grants to be executed: $ chmod u+x ../convert-tags.shand finally run it: $ ../convert-tags.shNow 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 donegive it the necessary grants to be executed: $ chmod u+x ../convert-branches.shand finally run it: $ ../convert-branches.shNow verify that the remote branches have been converted to local branches : $ git branch –a branch1 branch2 ... * masterOptionally, you can rename the master branch to main as follows: $ git branch -m master mainStep 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/svnStep 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.gitIf 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 mainStep 8. Push the repository Almost done. The last step is to upload the repository to the remote: $ git push --set-upstream --all originCongratulations, you have completed the migration! |
CopyRight 2018-2019 实验室设备网 版权所有 |