简体   繁体   English

Git SVN和合并分支

[英]Git SVN and merging branches

I am working on a svn project with two branches, lets call them 我正在开发一个带有两个分支的svn项目,让我们调用它们

trunk
branches/foo

My idea is to clone the whole svn repository (telling git which folder are trunk, tags and branches), do the merge in git and then copy my merge to a svn working copy and commit the changes from svn. 我的想法是克隆整个svn存储库(告诉git哪个文件夹是主干,标签和分支),在git中进行合并然后将我的合并复制到svn工作副本并从svn提交更改。

In this workflow, will I be able to use gits merging power or will that only work for branches created with git itself? 在这个工作流程中,我是否可以使用gits合并功能,还是仅适用于使用git创建的分支?

Create alias for checkout command: 为checkout命令创建别名:

git config alias.co checkout

Make sure that you local branches are up to date: 确保您的本地分支机构是最新的:

git co master    # checkout branch that tracks subversion's trunk
git svn rebase 
git co local/foo # checkout branch that tracks subversion's branches/foo
                 # It assumes that  the branch is created with the command:
                 # `git co -b local/foo remotes/foo`
                 # And the repo was created with:
                 # `git svn clone --stdlayout SVN_REPO_URL`
git svn rebase 

Merge branches: 合并分支:

# create new local branch based on `master`
git co master
git co -b merging_branch_foo 
# merge, resolve conflicts, etc (pure git)
git merge local/foo  

# rebase `merging_branch_foo` to linearize history for subversion
git rebase master # or `rebase -i`

# merge `merging_branch_foo` into `master`
git co master
git merge merging_branch_foo # --squash to create single commit

# commit changes to svn
git svn dcommit

# (optionally) delete `merging_branch_foo`
git branch -D merging_branch_foo

There is a way to perform merging with git but committing (upstream) with Subversion that is complicated to set up, but is powerful (and much easier than merging with Subversion!) in practice. 有一种方法可以执行与git的合并,但是使用Subversion提交(上游)设置很复杂,但实际上功能强大(并且比使用Subversion合并要容易得多!)。 First, read Derick Bailey's git+svn overview , because you will need to set up the git and SVN ignore files as he instructs. 首先,阅读Derick Bailey的git + svn概述 ,因为你需要按照他的指示设置git和SVN忽略文件。

Note that this doesn't use the standard git-svn package, but replicates a lot of what that does, manually. 请注意,这使用标准的git-svn软件包,而是手动复制许多软件。 If you're already using git-svn, don't use this method. 如果您已经在使用git-svn,请不要使用此方法。 Also, it's only worth using this method if you'll be repeatedly merging from the branch to the trunk (and especially if cherry-picking from the trunk to the branch) because that takes advantage of git's history when performing additional merges. 此外,如果您将反复从分支合并到主干(尤其是从主干到分支的挑选),这是唯一值得使用此方法,因为这会在执行其他合并时利用git的历史记录。

Then, the basic steps are as follows: 然后,基本步骤如下:

  1. SVN Checkout /trunk/ to a working copy folder; SVN Checkout /trunk/到工作副本文件夹; I'll assume it's C:\\trunk . 我假设它是C:\\trunk
  2. git init a git repository in that folder; git init该文件夹中的git存储库; set up .gitignore ; 设立.gitignore ; git add -A ; git add -A ; git commit (see git+svn above). git commit (参见上面的git + svn )。
  3. Create a git clone of the repository (in a different folder): git clone C:\\trunk foo . 创建存储库的git克隆(在不同的文件夹中): git clone C:\\trunk foo I'll assume this clone is in C:\\foo . 我假设这个克隆在C:\\foo
  4. Delete everything in C:\\foo except the .git subfolder, then SVN Checkout /branches/foo in C:\\foo . 删除C:\\foo.git子文件夹以外的所有内容,然后删除C:\\foo的SVN Checkout /branches/foo C:\\foo
  5. In C:\\foo, run git add -A; git commit 在C:\\ foo中,运行git add -A; git commit git add -A; git commit to save the changes on the branch to the git repository. git add -A; git commit将分支上的更改保存到git存储库。 This creates the initial git history that diverges from the history in C:\\trunk. 这会创建与C:\\ trunk中的历史不同的初始git历史记录。

We now have two folders that are both git repositories and Subversion working copies; 我们现在有两个文件夹,它们都是git存储库和Subversion工作副本; additionally, git thinks the folders are clones of the same repository. 另外,git认为文件夹是同一个存储库的克隆。

Perform work in the C:\\trunk and C:\\foo folders (or just svn update to get others' work). C:\\trunkC:\\foo文件夹中执行工作(或者只是svn update以使其他人工作)。 Periodically, run git add -A; git commit 定期运行git add -A; git commit git add -A; git commit to save changes to your git repositories. git add -A; git commit保存对git存储库的更改。

Now you want to merge the foo branch back into trunk. 现在你想将foo分支合并回trunk。 In C:\\trunk, run git pull C:\\foo . 在C:\\ trunk中,运行git pull C:\\foo This pulls in and merges all the changes from the C:\\foo folder, which is your git repository tracking the /branches/foo Subversion branch. 这将从C:\\foo文件夹中提取并合并所有更改,这是您的git存储库跟踪/branches/foo Subversion分支。 If necessary, resolve any conflicts and finish the git commit. 如有必要,解决所有冲突并完成git提交。

You can now commit the changes in C:\\trunk to Subversion without having to use Subversion to perform the merge. 您现在可以将C:\\ trunk中的更改提交到Subversion,而无需使用Subversion来执行合并。

I would recommend you to use SmartGit for your SVN project. 我建议您将SmartGit用于SVN项目。 It has very good support for both cherry-picking merges and full merges, properly modifying svn:mergeinfo. 它非常好地支持cherry-picking合并和完全合并,正确修改svn:mergeinfo。

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

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