Git SVN 和合并分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4290855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:47:07  来源:igfitidea点击:

Git SVN and merging branches

svngitgit-svn

提问by unkownt

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 本身创建的分支?

回答by jfs

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

回答by Bradley Grainger

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. First, read Derick Bailey's git+svn overview, because you will need to set up the git and SVN ignore files as he instructs.

有一种方法可以与 git 合并,但在实践中使用 Subversion 提交(上游),虽然设置起来很复杂,但功能强大(而且比与 Subversion 合并要容易得多!)。首先,阅读 Derick Bailey 的git+svn 概述,因为您需要按照他的指示设置 git 和 SVN 忽略文件。

Note that this doesn'tuse the standard git-svn package, but replicates a lot of what that does, manually. If you're already using git-svn, don't use this method. 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-svn 包,而是手动复制了很多功能。如果您已经在使用 git-svn,请不要使用此方法。此外,如果您将重复从分支合并到主干(尤其是从主干到分支的樱桃采摘),则仅值得使用此方法,因为这在执行额外合并时利用了 git 的历史记录。

Then, the basic steps are as follows:

然后,基本步骤如下:

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

We now have two folders that are both git repositories and Subversion working copies; additionally, git thinks the folders are clones of the same repository.

我们现在有两个文件夹,它们都是 git 存储库和 Subversion 工作副本;此外,git 认为这些文件夹是同一个存储库的克隆。

Perform work in the C:\trunkand C:\foofolders (or just svn updateto get others' work). Periodically, run git add -A; git committo save changes to your git repositories.

C:\trunkC:\foo文件夹中执行工作(或只是svn update为了获取其他人的工作)。定期运行git add -A; git commit以保存对 git 存储库的更改。

Now you want to merge the foo branch back into trunk. In C:\trunk, run git pull C:\foo. This pulls in and merges all the changes from the C:\foofolder, which is your git repository tracking the /branches/fooSubversion branch. If necessary, resolve any conflicts and finish the git commit.

现在您想将 foo 分支合并回主干。在 C:\trunk 中,运行git pull C:\foo. 这会引入并合并C:\foo文件夹中的所有更改,该文件夹是您跟踪/branches/fooSubversion 分支的git 存储库。如有必要,解决所有冲突并完成 git commit。

You can now commit the changes in C:\trunk to Subversion without having to use Subversion to perform the merge.

您现在可以将 C:\trunk 中的更改提交到 Subversion,而无需使用 Subversion 执行合并。

回答by Dmitry Pavlenko

I would recommend you to use SmartGitfor your SVN project. It has very good support for both cherry-picking merges and full merges, properly modifying svn:mergeinfo.

我建议您将SmartGit用于您的 SVN 项目。它对挑选合并和完全合并都有很好的支持,正确修改 svn:mergeinfo。