简体   繁体   English

Git与SVN主干和分支机构

[英]Git with SVN trunk and branches

Since I've been using Git, I liked the fact that you can create a local branch which you can squash to your master branch and DCommit to SVN (on trunk), and then delete that local branch (on Git). 由于我一直在使用Git,我喜欢你可以创建一个本地分支,你可以压缩到你的主分支和DCommit到SVN(在主干上),然后删除那个本地分支(在Git上)。

Now I want to find out how to deal with SVN branches. 现在我想了解如何处理SVN分支。 I need to work with a team on a new feature but we don't want to do that on Trunk. 我需要与团队合作开发新功能,但我们不想在Trunk上这样做。 So obviously we need to branch from trunk and work on that branch till we're happy with the work and merge back to Trunk. 所以显然我们需要从主干分支并在该分支上工作,直到我们对工作感到满意并合并回Trunk。

Any ideas on how to do that from a Git front-end perspective? 关于如何从Git前端角度做到这一点的任何想法? I'm sure there are plenty of resources out there but I haven't found something that explains that to me. 我确信那里有足够的资源,但我没有找到能够解释这一点的东西。

So just to make sure I explain myself, I know how to use Git with SVN trunk alone. 所以为了确保我解释自己,我知道如何单独使用Git和SVN中继。 That's easy and good. 这很容易也很好。 But I need to branch on SVN from trunk and use Git to work on that branch (while hopefully still having access to SVN trunk with Git) till the work is done and then merge that branch back to trunk on SVN (again using Git). 但是我需要从trunk上分支SVN并使用Gi​​t在该分支上工作(希望仍然可以使用Git访问SVN中继),直到工作完成,然后将该分支合并回SVN上的trunk(再次使用Git)。 If anyone can give me insight to that, I'd be VERY grateful! 如果有人能让我对此有所了解,我将非常感激! Is it possible to have that within a single Git repo? 是否可以在一个Git仓库中拥有它?

Thanks in advance. 提前致谢。 (If it's still not clear please let me know). (如果还不清楚请告诉我)。

I blogged about this topic a few months ago: http://www.jillesvangurp.com/2012/08/04/git-presentation/ 几个月前我在这个主题上发表了博客: http//www.jillesvangurp.com/2012/08/04/git-presentation/

In short, you can track multiple svn branches in a single git repository but you really shouldn't. 简而言之,您可以在单个git存储库中跟踪多个svn分支,但实际上您不应该这样做。 Having messed up on one occasion I would basically never ever try tracking multiple svn branches in one repository again. 有一次搞砸了我基本上再也不会尝试在一个存储库中跟踪多个svn分支。 It's just too volatile and too easy to major damage in svn. 它太易挥发,太容易在svn中造成严重损坏。 Basically everything seemed fine locally and I dcommitted my changes. 基本上一切都在当地很好,我提出了我的改变。 But actually I reordered some commits that really shouldn't have been reordered and did a lot of damage upstream. 但实际上我重新排序了一些真正不应该重新排序并在上游造成很多损害的提交。 In the end I had to do a big svn revert. 最后我不得不做一个大的svn还原。 Very embarrassing. 非常尴尬。

So, instead work with separate git repositories to track each svn branch and use those repositories as a remote on a git only repository where you actually work pushing to the different remote repositories as you go. 因此,使用单独的git存储库来跟踪每个svn分支,并将这些存储库用作仅在git存储库上的远程存储库,您可以在其中实际工作时推送到不同的远程存储库。 Isolating git and svn like this makes a lot of sense. 像这样隔离git和svn很有意义。 Make sure to keep your history linear (svn doesn't do non linear) and make sure to always rebase against upstream. 确保您的历史记录保持线性(svn不做非线性)并确保始终对上游进行折旧。

A safer way to move commits between branches/repositories (same thing in the git world) in this context is to use git format-patch and git am. 在这种情况下,在分支/存储库之间移动提交(在git世界中同样的事情)的更安全的方法是使用git format-patch和git am。 Alternatively, you can use git cherry-pick inside a repository. 或者,您可以在存储库中使用git cherry-pick。

I have finally discovered what I need to do in order to work with SVN branches using Git. 我终于发现了为了使用Git处理SVN分支所需要做的事情。 I have tried the stuff mentioned below (except where I specify) so I am fairly confident that as long as I don't do anything funny or out of line, my commits from Git to SVN should work just fine. 我已经尝试了下面提到的东西(除了我指定的地方)所以我相当自信,只要我不做任何有趣或不合适的事情,我从Git到SVN的提交应该工作得很好。 But of course I don't take any responsibility for any problems that might occur! 但是,对于可能发生的任何问题,我当然不承担任何责任! I am open for further comments / remarks. 我愿意接受进一步的评论/评论。

Even if I don't clone the SVN repo with the standard layout, I should have remotes configured that are pointing to the relevant SVN paths on subversion. 即使我没有使用标准布局克隆SVN repo,我也应该配置指向subversion上的相关SVN路径的遥控器。

To clone a SVN branch (the SVN path should be pointing to the root path containing trunk, branches and tags): 要克隆SVN分支(SVN路径应指向包含主干,分支和标记的根路径):

git svn clone -s http://myrepo.com c:/my/local/path

If you are already have a clone of an SVN branch you need to manually add remotes to other SVN branches, check these out: 如果您已经拥有SVN分支的克隆,则需要手动将遥控器添加到其他SVN分支,请检查以下内容:

What is important is that you have a local branch that is tracking the remote branch. 重要的是,您有一个跟踪远程分支的本地分支。

git checkout -b localbranch remotes/remote_branch

Now you can commit against that local branch and each time when done do this just like in trunk: 现在你可以针对那个本地分支进行提交,每次完成时就像在trunk中一样:

git svn fetch
git svn dcommit

When you are finally done with that SVN branch and you would like to merge to trunk, you can do the following (it takes everything from that branch and rebases it into master): 当你最终完成SVN分支并且想要合并到trunk时,你可以执行以下操作(它从该分支获取所有内容并将其重新绑定到master):

git checkout master
git merge localbranch
git rebase -i trunk  (just save the message)
git svn fetch
git svn dcommit

You can also just do a squash merge if you want to (if you don't want to bring all those little check-ins accross): 如果你愿意,你也可以直接进行壁球合并(如果你不想带来所有那些小签到):

git merge --squash localbranch
git svn fetch
git svn dcommit

You might be doing a dry-run before doing a real DCOMMIT to make sure everything is fine (including the destination). 在进行真正的DCOMMIT之前,您可能正在进行干运行,以确保一切正常(包括目的地)。

git svn dcommit -dry-run

However, if you anticipate merge conflicts (this is what I read on the web but I haven't tried it which I'm just mentioning) do the following before the git checkout master line shown above. 但是,如果您预计合并冲突(这是我在网上看到的,但我还没有尝试过,我只是提到它)在上面显示的git checkout master主线之前执行以下操作。 This is a local branch that doesn't track the remote branch which should be used to tackle merge conflicts and stuff. 这是一个本地分支,它不跟踪应该用于解决合并冲突和内容的远程分支。 Whether this is necessary is another story. 这是否必要是另一回事。

git checkout -b merge_work master
git merge localbranch
git checkout master
git rebase merge_work

I'm writing this down for myself and for other people who might want to try this in the future. 我正在为自己和其他可能希望将来尝试这一点的人写下这篇文章。

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

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