简体   繁体   English

Git 将分支从 svn 添加到远程

[英]Git add branches from svn to remote

TL;DR TL;博士

I want to not only move the trunk, but also all branches to the git remote I'm migrating from SVN to.我不仅要移动主干,还要移动所有分支到git远程我从 SVN 迁移到。


We have an SVN repository with a lot of branches which we want to migrate to git .我们有一个 SVN 存储库,其中包含许多我们想要迁移到git的分支。 For test (and demonstrational) purposes I have set up a small SVN repository that I'm now migrating to git .出于测试(和演示)目的,我设置了一个小型 SVN 存储库,我现在正在迁移到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.这成功创建了一个本地 git 存储库,其中包含 SVN 存储库的主干。 However, there was only one local branch:但是,只有一个本地分支:

$ git branch
* master

But git still knew about the other branches:但是git仍然知道其他分支:

$ 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远程 - 所以,在创建远程仓库并将我的主分支推送到它之后,我尝试了:

$ 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).乍一看,它似乎有效,但事实是在我的远程仓库中没有创建任何分支 - 相反,分支似乎已被推送到svn_origin而不是origin (这是我的git )。

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.这只获取了我已经拥有的master分支。 That seems to be caused by the SVN remote not being a normal remote:这似乎是由 SVN 遥控器不是普通遥控器引起的:

$ 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?但肯定有更简单的方法吗?

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.但是,这并不是我要求的更简单的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM