how to convert svn repository to git on Windows

前端 未结 4 1299
陌清茗
陌清茗 2021-02-09 18:17

We have remote svn repository and we want it to convert to git. Could you please tell me how is it possible to do it on Windows? Thank.

4条回答
  •  醉酒成梦
    2021-02-09 18:59

    I ended up doing this so much, I made a batch script to help out:

    usage: SvnToGit

    Prerequisites

    • You should already have the URL to the empty git remote repository.
    • You should have a pre-existing authors.txt file at the root ready to go.
    • Unless Git understands your svn format, you will need to run svnserve to 'serve' the repo. You can create a window service to do, something like this:

      c:>sc create svnserve binpath="\"svnserve.exe\" --service -r C:\Users\UserName\Repositories\Svn" displayname="Subversion Server" depend=Tcpip start=auto

    Caveats:

    • Check the intermediate results, occasionally something may fail
      (especially svn clone).
    • Note the section for manual changes if needed for tags. Perhaps, this could be done in batch file, but it seemed complex and not a high priority for me. There is a PAUSE here, so
      you can make these changes manually.
    • I have not tested this on many systems, you might need to tweak the script a bit to adjust to your system.

    The actual batch file:

    REM Argument 1: Path to your repository
    REM Argument 2: Path to your new Git working directory
    REM Create authors.txt file
    REM It will contain lines like:
    REM SomeCoder = Some Guy 
    REM If Git is now aware of the format of svn repository, you will need to use svnserve:
    REM svnserve -d -R --root path/to/your/repository
    PAUSE
    
    IF EXIST bare.git\NUL RD /S /Q bare.git
    IF EXIST GitTemp\NUL RD /S /Q GitTemp
    
    REM The following will not work, if SVN is using a newer FS than what Git is aware of
    REM git svn clone file:///%1 --prefix=svn/ --no-metadata -A authors.txt --stdlayout GitTemp
    REM So, use
    git svn clone svn://localhost/%1 --prefix=svn/ --no-metadata -A authors.txt --stdlayout GitTemp
    PAUSE
    
    REM GitIgnore
    cd GitTemp
    git svn show-ignore > .gitignore
    git add .gitignore
    git commit -m "Convert svn:ignore properties to .gitignore."
    cd ..
    
    REM Bare Repo
    git init --bare bare.git
    cd bare.git
    git symbolic-ref HEAD refs/heads/trunk
    cd ..
    
    cd GitTemp
    git remote add bare ../bare.git
    git config remote.bare.push 'refs/remotes/*:refs/heads/*'
    git push bare master
    cd ..
    
    REM clean up SVN type stuff
    cd bare.git
    REM git branch -m trunk master
    git branch -m svn/trunk master
    git symbolic-ref HEAD refs/heads/master
    cd ..
    
    REM Manual changes if needed for tags
    REM git for-each-ref --format='%(refname)' refs/heads/tags |
    REM cut -d / -f 4 |
    REM while read ref
    REM do
    REM   git tag "$ref" "refs/heads/tags/$ref";
    REM   git branch -D "tags/$ref";
    REM done
    PAUSE
    
    REM Working Directory
    git clone bare.git %2
    
    cd %2
    git checkout master
    
    git remote remove origin
    PAUSE
    
    git remote add origin %3
    REM Then
    git push -u origin master
    

提交回复
热议问题