简体   繁体   English

Git分支致力于开发,同时将一些更改合并到主版本中

[英]Git branches for working on dev while merging some changes into the master

Previously, using SVN, I would have a branch that was my development branch, which matched the code on the development site. 以前,使用SVN,我将拥有一个分支,即我的开发分支,该分支与开发站点上的代码匹配。 Once in a while, I would make a change in the development branch and then merge just the change into the trunk so I could put it on production. 有时,我会在开发分支中进行更改,然后将更改仅合并到主干中,以便将其投入生产。 I'm wondering how I might accomplish something similar with git. 我想知道如何用git完成类似的工作。

Basically, I want to be able merge 1 or a few commits from the branch into the master without merging the entire branch. 基本上,我希望能够将分支中的1个或几个提交合并到主服务器中,而不合并整个分支。

Or should I be working with git differently? 还是应该以其他方式使用git? (I need to release the changes right away so I can't way till all the changes are done.) Should I be working with multiple branches? (我需要立即发布更改,以便在所有更改完成之前无法发布。)我应该使用多个分支吗? Can I merge 1 branch into multiple other branches? 我可以将一个分支合并到多个其他分支吗?

You've almost answered your question: yes, you can merge one branch into multiple other branches. 您几乎回答了您的问题:是的,您可以将一个分支合并为多个其他分支。

So, what you want to do is create a branch just for this feature/bugfix (the common name is "topic"), starting from a common ancestor of all branches you'll want to merge it into. 因此,您要做的就是为此功能/错误修正创建一个分支(通用名称为“ topic”),从您要合并到其中的所有分支的共同祖先开始。 Do your work, commit it, then merge it into all of those. 做您的工作,提交它,然后将其合并到所有这些中。

# say version 1.2.3 is a common ancestor, and it's tagged
git checkout -b bugfix v1.2.3

# do some stuff
git add ...
git commit

git checkout master
git merge bugfix

git checkout dev
git merge bugfix
...

The key part here is to make sure to start your branch at a common ancestor . 这里的关键部分是确保从一个共同的祖先开始分支 If you don't, you'll end up merging pieces of other things in as well. 如果不这样做,您最终也将合并其他内容。

If for some reason it's difficult to find a good common ancestor, you can fall back to cherry-picking . 如果由于某种原因很难找到一个好的共同祖先,则可以退而求其次 This essentially copies a commit from one place to another. 这实质上是将提交从一个地方复制到另一个地方。 It's best to avoid it when you can, though, since it does mean you end up with two copies of a commit in the history. 但是,最好避免使用它,因为这确实意味着您将在历史记录中获得两次提交的副本。

I prefer to make "topic branches" in git, where I implement a feature in its own branch. 我更喜欢在git中创建“主题分支”,在该分支中实现其功能。 Then I can choose if I merge it into development or production or both. 然后,我可以选择是否将其合并到开发或生产中,或两者都合并。 This allows feature development to continue, while bug fixes can still be applied to production in a reasonable amount of time. 这样可以继续进行功能开发,同时仍可以在合理的时间内将错误修复应用于生产。

Its similar in concept to the model explained here: http://nvie.com/posts/a-successful-git-branching-model/ 其概念与此处解释的模型类似: http : //nvie.com/posts/a-successful-git-branching-model/

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

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