GIT-SVN: Using git to manage SVN code

Source: Internet
Author: User
Tags svn update using git

Brief introduction

Both SVN and git are common version management software, but git is more advanced than SVN in terms of concept or functionality. But some companies are using SVN as the central repository, and Git and SVN code synchronization can be done by git-svn this software, in order to use Git to manage the SVN code. The final effect is equivalent to using the SVN repository as a remote repository for git, and your local code is managed by Git, and your local commit is synced to SVN only when you push to SVN.

Clone from SVN

First look at the SVN project structure used for testing, the SVN repository path file:///d/Projects/svn_repo , which can be svnadmin create svn_repo created with the command. The warehouse has 2 branches and 1 tags, which belong to the SVN standard layout.

SVN Project structure:

 1 
2
3
4
5
6
7
8
9
10
11
12
13
 /d/proj1 
├──branches
│  ├──a
│  │  └──readme │  └──b
│  ├──< Span class= "number" >11.txt
│  └──readme.txt
├──tags
│  └──v1. 0
│  ├──11.txt
│  └──readme.txt
└──trunk
Span class= "line" >└──readme.txt

Command format: git svn clone <svn仓库路径> [本地文件夹名] [其他参数] equivalent togit clone
Example:git svn clone file:///d/Projects/svn_repo proj1_git -s --prefix=svn/
Parameter description:

    • -sTell Git that the Subversion repository follows the basic branch and tag naming rules, which are standard layouts.
      If your trunk (trunk, which is equivalent to the master branch in non-distributed version control, represents the main line of development), the Branch (branches) or tags (tags) are named in different ways, you should make a corresponding change.
      -sThe arguments are actually -T trunk -b branches -t tags abbreviations, which tell git the correspondence between these folders and the Git branch, tag, and master.
    • --prefix=svn/Adds a prefix svn to all of the remote names of SVN, which is more uniform and can preventwarning: refname ‘xxx‘ is ambiguous.

Now, look at the project with GIT-SVN cloning (running git branch-a), where Git's branching is in the SVN folder.

1
2
3
4
5
* Master
Remotes/svn/a
Remotes/svn/b
Remotes/svn/tags/v1. 0
Remotes/svn/trunk

Only download history after the specified version

If the number of commits on SVN is very great, git svn clone will be very slow, typically more than hundreds of versions will be about 10 minutes. You can download only a few versions of clone at this time,
Command:git svn clone -r<开始版本号>:<结束版本号> <svn项目地址> [其他参数]
Example:git svn clone -r2:HEAD file:///d/Projects/svn_repo proj1_git -s
Note: Where 2 is the SVN version number, head represents the latest version number, that is, only download the SVN server version 2 to the latest version of the code.

Work flow

Simply put, the first new branch will record the tracing relationship with the SVN remote counterpart, and all of your commits are local, and there is no difference between the pure git-managed project and git svn rebase git svn dcommit the SVN repository at the same time.

General work flow (recommended)
    1. New Branchgit checkout -b <本地分支名称> <远程分支名称>
      Example:git checkout -b a svn/a
      Description: A new local branch A was created here, corresponding to the a branch of SVN.
    2. Work locally, commit to the corresponding branch
    3. git svn rebaseUpdate the code from SVN, equivalent to the SVN update.
    4. git svn dcommitCommit your commit to the SVN remote repository and it is recommended to run Git svn rebase before committing.
Where other branches of git work
    1. git chechout -b a svn/aA new local branch A is created here, corresponding to the a branch of SVN.
    2. git checkout -b feature1On the basis of a branch, open a local Feture1 branch
    3. Development in the Feture1 branch, with multiple commits
    4. On the Feture1 branch git svn rebase and git svn dcommit so the Feature1 commit is committed to the SVN a branch.
      It is important to remember which branch the FETURE1 is checkout from, and which is the same as the SVN remote branch. For example, here is a branch, then the SVN branch is Svn/a,commit will be submitted to the SVN branch A.
SVN Branch Management new branch to SVN

Command:git svn branch <分支名称>
Example:git svn branch c_by_git
Description: Built a c_by_git branch on the SVN repository
Branching situation

1
2
3
4
5
6
7
  A
* Master
Remotes/svn/a
Remotes/svn/b
Remotes/svn/c_by_git
Remotes/svn/tags/v1. 0
Remotes/svn/trunk

Delete SVN branch
    • Delete SVN Branch Directorysvn rm <svn分支路径> -m <commit信息>
      Example:svn rm file:///d/Projects/svn_repo/branches/c_by_git -m ‘rm branch‘
    • Delete a remote trace branchgit branch -D -r <远程分支名称>
      Example:git branch -D -r svn/c_by_git
SVN on tag Management new tag

Command:git svn tag <tag名称>
Example:git svn tag v1.1
Description: Built a v1.1tag on the SVN repository

Delete tag
    1. Delete SVN directorysvn rm <svntag路径> -m <commit信息>
      Example:svn rm file:///d/Projects/svn_repo/tags/v1.1 -m ‘rm tag‘

    2. Delete a remote trace branchgit branch -D -r <远程分支名称>
      Example:git branch -D -r svn/tags/v1.1
      Description: SVN's tag and branch appear to be the same in git, so git branch is used here.

Conflict resolution

If both local and SVN have been modified, you can't go fast and git svn rebase will get an error.
This should be done as follows:

    1. Manually modify the conflicting files after the modification is completegit add

    2. git rebase --continue

    3. git svn dcommit

SVN does not comply with the specification of the situation

the above is the SVN warehouse is the standard situation, if not standard, the following places will be different. Basically, each step has to add the exact path of SVN.
First look at the structure of the sample project, the warehouse path is file:///d/Projects/svn_repo2 . The main branch of this project is the Dev folder, and the Branch1 and Tag1 folders represent a branch and tag respectively.

SVN Project structure:

1
2
3
4
5
6
7
/d/proj2
├──branch1
│└──file1.txt
├──dev
│└──file1.txt
└──tag1
└──file1.txt

Clone from SVN

Command:git svn clone <svn项目地址,要包含具体分支路径> [本地文件夹名]
Example:git svn clone file:///d/Projects/svn_repo2/dev proj2_svn

Add Remote Branch Information

Command:

    1. git config --add svn-remote.<远程分支名称>.url <svn地址,要包含具体分支路径>
    2. git config --add svn-remote.<远程分支名称>.fetch :refs/remotes/<远程分支名称>

Example:

    1. git config --add svn-remote.svn/branch1.url file:///d/Projects/svn_repo2/branch1
    2. git config --add svn-remote.svn/branch1.fetch :refs/remotes/svn/branch1

Note: the "remote Branch name" can be filled out here, as long as these three remain consistent. It is recommended that svn/ you prefix them so that all of the SVN branches appear consistent, similar to the previous clone --prefix=svn/ .

New local branch, corresponding to SVN

Command:

    1. git svn fetch <远程分支名称>Get the code for this branch of the SVN repository
    2. git checkout -b <本地分支名> <远程分支名称>

Example:

    1. git svn fetch svn/branch1
    2. git checkout -b branch1 svn/branch1

Branch situation:

1
2
3
4
* BRANCH1
Master
Remotes/git-svn
Remotes/svn/branch1

GIT-SVN: Using git to manage SVN code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.