Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

Git add branches from svn to remote

TL;DR

I want to not only move the trunk, but also all branches to the git remote I'm migrating from SVN to.


We have an SVN repository with a lot of branches which we want to migrate to git. For test (and demonstrational) purposes I have set up a small SVN repository that I'm now migrating to git. Therefore I started with

$ git svn init -t tags -T trunk -b branches --prefix=svn_origin/ svn://localhost/
$ git svn fetch

This successfully created a local git repo containing the trunk of the SVN repo. However, there was only one local branch:

$ git branch
* master

But git still knew about the other branches:

$ git branch -a
* master
  remotes/origin/master
  remotes/svn_origin/Branch%2001
  remotes/svn_origin/trunk

As I don't care about the local repository for now, I then wanted to push these remote branches to the new git remote - so, after creating the remote repo and pushing my master branch to it, I tried:

$ git push --all
Everything up-to-date

So this obviously didn't work. I then tried:

$ git push origin '*:*'
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 441 bytes | 220.00 KiB/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/MetaColon/SVN.git
 * [new branch]      origin/master -> origin/master
 * [new branch]      svn_origin/Branch%2001 -> svn_origin/Branch%2001
 * [new branch]      svn_origin/trunk -> svn_origin/trunk

On first glance it seems as if it worked, but the truth is that no branch was created in my remote repo - instead the branches seem to have been pushed to svn_origin instead of origin (which is my git repo).

I then tried something I had found here for fetching all branches to my local repo (I could then push them to my remote):

$ git branch -r | grep -v '->' | while read remote; do git branch --track "${remote#svn_origin/}" "$remote"; done
Branch 'origin/master' set up to track remote branch 'master' from 'origin'.
fatal: Cannot setup tracking information; starting point 'svn_origin/Branch%2001' is not a branch.
fatal: Cannot setup tracking information; starting point 'svn_origin/trunk' is not a branch.

This only fetched the master branch, which I already had. That seems to be caused by the SVN remote not being a normal remote:

$ git remote
origin

It is at that point that the only remaining option seems to be checking out each branch manually (or via a script) and then pushing all branches from the local repository. But surely there is a simpler way?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I wrote a script that seems to do the job:

#!/bin/bash

identifier="svn_origin/"
idLen=`expr ${#identifier} + 3`

git branch -r | grep $identifier | cut -c $idLen- | while read branch; do
    echo "Branch is $branch"
    echo "Executing git checkout -b $branch $identifier$branch"
    git checkout -b $branch $identifier$branch
done

echo "Pulled all branches..."

echo "Checking out master"
git checkout master
echo "Pushing all branches to remote"
git push --all

read -p "Done - press ENTER to exit"

However, this is not quite the simpler way I was asking for.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...