简体   繁体   English

使用git rebase和git merge建立良好的团队工作流程

[英]Using git rebase and git merge to set up a good team workflow

I know that there's plenty of similar questions, but I couldn't find a good answer that helps me to propose a good solution in the company where I work. 我知道有很多类似的问题,但是找不到一个好的答案可以帮助我在我工作的公司中提出一个好的解决方案。 We're not a lot of developers, but I would like to propose a scalable workflow. 我们不是很多开发人员,但是我想提出一个可扩展的工作流。

The (not so) hypothetical case 假设情况(并非如此)

The situation is the most common one: there is a master branch that never receives direct commits. 这种情况是最常见的情况:存在一个master分支,它永远不会收到直接提交。 If I need to do something I create a feature/personal branch (usually long-live personal branches was used more often than fats feature ones). 如果需要执行某些操作,则可以创建feature/personal分支(通常使用长寿的个人分支比使用胖功能的分支更多)。 Once I feel comfortable with the code I created, I want to bring it back to master (which, in meantime, has received another commits). 一旦对所创建的代码感到满意,我想将其重新带回master (与此同时, master又收到了一次提交)。

It's important to point up that both master and branchX are always pushed to remote. 重要的是要指出, masterbranchX总是被推到远程。

So, to graphically clarify it, we are in this situation (I'll use C for commits, M for merges): 因此,为了以图形方式澄清它,我们处于这种情况下(我将使用C进行提交,使用M进行合并):

branch1 C2---C3---C6---C9--- / master C0---C1---C4---C5---C7---C8

Current Workflow 目前的工作流程

The current used workflow can be defined merge-up/merge-down : since I don't want to fix merge conflicts in master, first I merge-up master inside branch1 , and then I merge-down branch1 in master . 当前使用的工作流程可以被定义合并升/合并向下 :因为我不希望修复主合并冲突,首先我上合并了 masterbranch1 ,然后我合并向下 branch1master

branch1 C2---C3---C6---C9---M1 / /\\ master C0---C1---C4---C5---C7---C8 M2

Doing so, I resolve the conflict inside the branch and then I can merge down my branch into master. 这样做,我解决了分支内部的冲突,然后可以将分支合并到master中。

Personally I don't like this solution for two main reasons: 我个人不喜欢这种解决方案,主要有两个原因:

  • it leads to a very messy history tree 它导致一棵非常凌乱的历史树
  • merging a parent branch into a child branch sound so no-sense to me 将父分支合并到子分支的声音对我来说毫无意义

On the other hand, my coworkers argues that: 另一方面,我的同事认为:

  • it's easy to understand, even for not-expert internship coworkers 即使对于非专业的实习同事也很容易理解
  • it leads to fewer errors since you left untouched the history (unlike rebase) 由于您保持不变的历史记录(与重新设置不同),它导致的错误更少

Proposed solution 拟议的解决方案

What I proposed is the common and more straightforward rebase-before-merge-down workflow. 我提出的是通用且更直接的“合并前变基”工作流。

Once i want to merge branch1 in master , first I rebase the former on the latter, so i handle all the conflicts in my branch; 一旦我想合并branch1master ,第一我变基上,后者是前者,所以我处理我分支的所有冲突; than I merge-down branch1 into master (with NO-FF if the feature branch is meaningful, with FF otherwise) 比我 branch1 向下合并master (如果功能分支有意义,则使用NO-FF,否则使用FF)

branch1 C2---C3---C6 rebase / \\ master C5---C7---C8 M1

This solution, however, has a main drawback: 但是,此解决方案有一个主要缺点:

Since both are synced with the remote, a git push --force is necessary. 由于两者都与远程同步,因此需要git push --force So, if someone do something wrong (because he's in a hurry, distracted or silly), weeks of work could be lost in one second. 因此,如果某人做错了事(因为他急着,分心或愚蠢),那么可能会在一秒钟内损失数周的工作。

On the other hand, the advantages should be: 另一方面,优点应该是:

  • Keep the history tree very clean and meaningful 保持历史树非常干净和有意义
  • Flatten and remove useless branches, keeping only relevant ones 展平并删除无用的分支,仅保留相关的分支

So, the question? 那么,有什么问题吗?

Which scalable workflow do you adopt in your large team to keep the git history clean and meaningful, and, on the other side, prevent potential disasters like a wrong git push --force can do? 您在大型团队中采用了哪种可扩展的工作流程来保持git历史的整洁和有意义,另一方面,如何防止git push --force错误之类的潜在灾难?

One common model in my experience has been this: 根据我的经验,一个常见的模型是:

  • There is an authoritative master repository. 有一个权威的存储库。
    • The master branch here can only be updated via pull request (in your parlance, a down-merge). 这里的master分支只能通过拉取请求进行更新(用您的话来说就是向下合并)。
    • Whether you may force-push to other branches is determined on an ad-hoc basis (tends to be determined by how many people actually need to work on a branch simultaneously). 您是否可以强制推送到其他分支机构是临时确定的(往往由实际需要在同一分支上同时工作的人数决定)。
  • Everyone maintains their own fork of the master repository. 每个人都维护自己的主存储库分支。
    • They can force-push to their own branches whenever. 他们可以随时将其强制推送到自己的分支机构。
    • To merge changes into the master branch of the master repository, you rebase your branch onto master and pull request in. 要将更改合并到存储库的master分支中,可以将分支重新建立到master然后拉入请求。

Releases are much trickier to handle. 发布要难得多。

I would suggest looking at how open-source projects such as Linux, Git, Node, etc. maintain their repositories and take that into consideration. 我建议研究一下Linux,Git,Node等开源项目如何维护其存储库,并将其纳入考虑范围。

If branchX (such as branch1 ) is works for your own, you can use the way you want: rebase before merge. 如果branchX (例如branch1 )适合您自己,则可以使用所需的方式:合并前branch1

If branchX (such as branch1 ) is works for all developers, you'd better not use your preposed solution, the main drawback as you said other developers will be confused, and they may not find their own changes any more. 如果branchX (例如branch1 )适用于所有开发人员,则最好不要使用预设的解决方案,这是主要缺点,因为您说其他开发人员会感到困惑,并且他们可能再也找不到自己的更改了。

There is a way you can use: git merge branch1 --squash . 有一种可以使用的方法: git merge branch1 --squash Assume you git log is: 假设您的git log是:

branch1         C2---C3---C6---C9---
               /
master   C0---C1---C4---C5---C7---C8

After you execute git merge branch1 --squash , the git log will looks like: 执行git merge branch1 --squash ,git日志将如下所示:

branch1         C2---C3---C6---C9
               /
master   C0---C1---C4---C5---C7---C8---M1

This makes your master branch looks more clearly. 这使您的master分支看起来更清晰。 And If you want to develop new features you can checkout feature branches from master directly. 而且,如果您要开发新功能,则可以直接从master检出功能分支。

BTW: I need to correct the graph you used in your current workflow. 顺便说一句:我需要更正您在当前工作流程中使用的图表。 After merge master into branch1 , the graph looks like master合并到branch1 ,图看起来像

       C2---C3---C6---C9---M1  branch1
      /                   /
C0---C1---C4---C5---C7---C8  master

After merge branch1 into master , there is no commit M2 to create by default, it only a fast forward merge. branch1合并到master ,默认情况下没有要创建的提交M2 ,它只是快速向前合并。 Both branch1 and master are point to M1 commit now. branch1branch1master都指向M1提交。

       C2---C3---C6---C9---M1  branch1, master
      /                    /
C0---C1---C4---C5---C7---C8 

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

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