How do I migrate an SVN repository with history to a new Git repository?

前端 未结 30 2245
离开以前
离开以前 2020-11-22 02:51

I read the Git manual, FAQ, Git - SVN crash course, etc. and they all explain this and that, but nowhere can you find a simple instruction like:

SVN repository in: <

30条回答
  •  半阙折子戏
    2020-11-22 03:55

    We can use git svn clone commands as below.

    • svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > authors.txt

    Above command will create authors file from SVN commits.

    • svn log --stop-on-copy

    Above command will give you first revision number when your SVN project got created.

    • git svn clone -r:HEAD --no-minimize-url --stdlayout --no-metadata --authors-file authors.txt

    Above command will create the Git repository in local.

    Problem is that it won't convert branches and tags to push. You will have to do them manually. For example below for branches:

    $ git remote add origin https://github.com/pankaj0323/JDProjects.git
    $ git branch -a
    * master
      remotes/origin/MyDevBranch
      remotes/origin/tags/MyDevBranch-1.0
      remotes/origin/trunk
    $$ git checkout -b MyDevBranch origin/MyDevBranch
    Branch MyDevBranch set up to track remote branch MyDevBranch from origin.
    Switched to a new branch 'MyDevBranch'
    $ git branch -a
    * MyDevBranch
      master
      remotes/origin/MyDevBranch
      remotes/origin/tags/MyDevBranch-1.0
      remotes/origin/trunk
    $
    

    For tags:

    $git checkout origin/tags/MyDevBranch-1.0
    Note: checking out 'origin/tags/MyDevBranch-1.0'.
    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by performing another checkout.
    
    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -b with the checkout command again. Example:
    
      git checkout -b new_branch_name
    
    HEAD is now at 3041d81... Creating a tag
    $ git branch -a
    * (detached from origin/tags/MyDevBranch-1.0)
      MyDevBranch
      master
      remotes/origin/MyDevBranch
      remotes/origin/tags/MyDevBranch-1.0
      remotes/origin/trunk
    $ git tag -a MyDevBranch-1.0 -m "creating tag"
    $git tag
    MyDevBranch-1.0
    $
    

    Now push master, branches and tags to remote git repository.

    $ git push origin master MyDevBranch MyDevBranch-1.0
    Counting objects: 14, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (11/11), done.
    Writing objects: 100% (14/14), 2.28 KiB | 0 bytes/s, done.
    Total 14 (delta 3), reused 0 (delta 0)
    To https://github.com/pankaj0323/JDProjects.git
     * [new branch]      master -> master
     * [new branch]      MyDevBranch -> MyDevBranch
     * [new tag]         MyDevBranch-1.0 -> MyDevBranch-1.0
    $
    

    svn2git utility

    svn2git utility removes manual efforts with branches and tags.

    Install it using command sudo gem install svn2git. After that run below command.

    • $ svn2git --authors authors.txt --revision

    Now you can list the branches, tags and push them easily.

    $ git remote add origin https://github.com/pankaj0323/JDProjects.git
    $ git branch -a
      MyDevBranch
    * master
      remotes/svn/MyDevBranch
      remotes/svn/trunk
    $ git tag
      MyDevBranch-1.0
    $ git push origin master MyDevBranch MyDevBranch-1.0
    

    Imagine you have 20 branches and tags, obviously svn2git will save you a lot of time and that's why I like it better than native commands. It's a nice wrapper around native git svn clone command.

    For a complete example, refer my blog entry.

提交回复
热议问题