使用Git与SVN双向同步

使用git svn指令在Coding.net实现Git和SVN双向同步

当前目录为:drago@CWAY MINGW64 /d/user/desktop/02_公司/svn50 $
  1. $ mkdir localSvn
  2. $ cd localSvn/
    drago@CWAY MINGW64 /d/user/desktop/02_公司/svn50/localSvn $ 
  3. git svn clone https://svn.coding.net/sdjq/coding-devops-guide/yjptnet -s
    Initialized empty Git repository in D:/user/desktop/02_公司/svn50/localSvn/yjptnet/.git/
    r1 = 81191a4cd25782e06ae161a7bc03d47beaf31ebd (refs/remotes/origin/trunk)
    Checked out HEAD:
      https://svn.coding.net/sdjq/coding-devops-guide/yjptnet/trunk r1
  4. $ ls yjptnet
  5. cd yjptnet/
    drago@CWAY MINGW64 /d/user/desktop/02_公司/svn50/localSvn/yjptnet (master) $
  6. $ ls
  7. $ git status
    On branch master
    nothing to commit, working tree clean
  8. 创建新文件:D:\user\desktop\02_公司\svn50\localSvn\yjptnet\helloworld.txt
  9. $ git status
    On branch master
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            helloworld.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
    
  10. $ git add .
  11. $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged  <file>..." to unstage)
            new file:   helloworld.txt
    
  12. $ git commit -m ' init commit'
    [master 31e0c76]  init commit
     1 file changed, 68 insertions(+)
     create mode 100644 helloworld.txt
  13. $ git svn dcommit
    Committing to https://svn.coding.net/sdjq/coding-devops-guide/yjptnet/trunk ...
            A       helloworld.txt
    Committed r2
            A       helloworld.txt
    r2 = b58b9497ae4f92d81ef5fbe11c91017b7f443588 (refs/remotes/origin/trunk)
    No changes between 31e0c76ea96128297693594ea6cfb0b335a037a0 and refs/remotes/origin/trunk
    Resetting to the latest refs/remotes/origin/trunk
    

请严格按照下图步骤操作,实战中如果漏了步骤或顺序乱了,在最终提交时会出现怎么都消除不了的错误:Unable to determine upstream SVN information from HEAD history. Perhaps the repository is empty. at C:/Program Files/Git/mingw64/libexec/git-core\git-svn line 916.

指令汇总如下:

$ mkdir localSvn
$ cd localSvn/
$ git svn clone https://svn.coding.net/sdjq/coding-devops-guide/yjptnet -s
$ ls yjptnet
$ cd yjptnet/
$ ls
$ git status
创建文件helloworld.txt
$ git status
$ git add .
$ git status
$ git commit -m ' init commit'
$ git svn dcommit

以下是在Coding.net上使用成功的Jenkins脚本:

pipeline {
  agent any
  stages {
    stage('检出') {
      steps {
        checkout([
          $class: 'GitSCM',
          branches: [[name: GIT_BUILD_REF]],
          userRemoteConfigs: [[
            url: GIT_REPO_URL,
            credentialsId: CREDENTIALS_ID
          ]]])
          sh '''pwd
ls'''
        }
      }
      stage('自定义构建过程') {
        steps {
          echo '正在推送文件...'
          sh '''sudo apt-get --yes --allow-downgrades --allow-remove-essential --allow-change-held-packages install git-svn
pwd
ls
cd ..
mkdir localSVN
cd localSVN/
echo cqh123456. | git svn --username sd_cqh clone -s http://47.104.144.219:8080/svn/硬件驱动产品
ls 硬件驱动产品
cd 硬件驱动产品/
git status
rsync -av --progress /root/workspace/ /root/localSVN/硬件驱动产品/ --exclude .git
pwd
ls
git status
git add .
git status
git commit -m \' init commit\'
git svn dcommit'''
        }
      }
    }
  }
注:
  1. 上述有一步复制文件指令:rsync -av --progress /root/workspace/ /root/localSVN/硬件驱动产品/ --exclude .git.如果此步骤使用cp -r /root/workspace/. /root/localSVN/硬件驱动产品,会把.git文件夹一齐复制过去,会造成git HEAD 游离的异常状态,错误提示如下:这样就无法在master主干上添加新文件并成功提交,即使是git checkout master切换回主干也很麻烦还要合并融合等操作消除游离状态.
    [2022-03-25 22:29:43] + git status
    [2022-03-25 22:29:44] HEAD detached at 5048d3e
    [2022-03-25 22:29:44] nothing to commit, working tree clean
  2. 指令中的@在url中需要写成%40的UTF8形式,如: git clone https://12293669%40qq.com:D123456@e.coding.net/sdjq/paas_hardware/paasplugins.git base

参考资料

  1. Coding.net同步到公司自建SVN的Jenkin部份脚本

  2. 使用Git 作为Subversion 仓库的客户端

  3. git到svn双向同步的实现

  4. 一个项目同时用svn和git进行版本控制

  5. GIT版本控制— GIT与SVN的相互转换(三)

  6. git-svn dcommit error: unable to determine upstream SVN information,仅供参考,实战中无效

    ,git-svn: Unable to determine upstream SVN information from working tree history
  7. SVN或者GIT远程双向同步

  8. git-svn — 让git和svn协同工作

  9. gist:3149875

    :git-svn 使用,实战中一直报:Unable to determine upstream SVN information from HEAD history. Perhaps the repository is empty. at C:/Program Files/Git/mingw64/libexec/git-core\git-svn line 916.
  10. git detached HEAD解决方案(亲测)