如何正确使用 git 和分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10009175/
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-10 13:22:11  来源:igfitidea点击:

How to properly use git and branches

gitversion-controlbranch

提问by Marcx

I'm kind of new to version control with GIT. I read this Guideand am following the basic approach that is shown in the diagram HERE. Still, I have some doubts about how to use git branches to separate the development of new features from existing code.

我对使用 GIT 进行版本控制有点陌生。我阅读了本指南,并遵循此处图表中显示的基本方法。尽管如此,我对如何使用 git branch 将新功能的开发与现有代码分开有些疑问。

Here is an example. Suppose that at the start, my repository contains the following two main branches:

这是一个例子。假设一开始,我的存储库包含以下两个主要分支:

  • Master branch (containing the release version)
  • Develop Branch (containing new fixes or features to separate them from existing project features)
  • Master 分支(包含发布版本)
  • 开发分支(包含新的修复或功能以将它们与现有项目功能分开)

When I need to develop new features or modules, I create branches from Develop and start the new code projects there. For example, I make three new branches to add features related to Sun, Star, and SuperNova. Now, my repository contains five branches:

当我需要开发新功能或模块时,我从 Develop 创建分支并在那里开始新的代码项目。例如,我做了三个新的分支,以增加相关的功能SunStar以及SuperNova。现在,我的存储库包含五个分支:

  • Master branch: Release 1.0.0
  • Develop branch: Modification after release 1.0.0
  • NewModule_Sun branch: add Sun to project (create from Develop branch)
  • NewModule_Star branch: add Star to project (create from Develop branch)
  • NewModule_SuperNova branch: add SuperNova to Project (create from Develop branch)
  • 主分支:发布 1.0.0
  • Develop 分支:1.0.0 版本后修改
  • NewModule_Sun 分支:将 Sun 添加到项目中(从 Develop 分支创建)
  • NewModule_Star 分支:将 Star 添加到项目(从 Develop 分支创建)
  • NewModule_SuperNova 分支:将 SuperNova 添加到项目(从 Develop 分支创建)

For Release 1.0.1, I want to include the the Sunand Starmodules, but not SuperNova. So, I merge them with Develop and then merge Develop with the Release:

对于 1.0.1 版,我想包括SunStar模块,但不包括SuperNova. 因此,我将它们与 Develop 合并,然后将 Develop 与 Release 合并:

  1. Merge NewModule_Sun into Develop
  2. Merge NewModule_Star into Develop
  3. Merge Develop into Master (release 1.0.1)
  1. 将 NewModule_Sun 合并到 Develop
  2. 将 NewModule_Star 合并到 Develop
  3. 将 Develop 合并为 Master(版本 1.0.1)

The Develop branch needs to be kept permanently, but the Sunand Starbranches are no longer needed. They get deleted:

Develop 分支需要永久保留,但不再需要SunStar分支。他们被删除:

  1. Delete the NewModule_Sun branch
  2. Delete the NewModule_Star branch
  1. 删除 NewModule_Sun 分支
  2. 删除 NewModule_Star 分支

After these changes my repository contains the following three branches:

在这些更改之后,我的存储库包含以下三个分支:

  • Master Branch: Release 1.0.1
  • Develop Branch: Modification after release 1.0.1
  • NewModule_SuperNova branch: Modification after release 1.0.0 (created from Develop when it was not merged with the Star/Sun branches)
  • 主分支:发布 1.0.1
  • Develop Branch:1.0.1 版本后的修改
  • NewModule_SuperNova 分支:1.0.0 版本后修改(未与 Star/Sun 分支合并时从 Develop 创建)

==

==

Firstly, am I using git branches correctly?

首先,我是否正确使用了 git 分支?

Secondly, I reviewed the history of the final Develop branch, and it seems that I have lost some information on the NewModules. Is that normal? And, is it possible to transfer all the history information to the Develop branch?

其次,我回顾了最后一个 Develop 分支的历史,似乎丢失了一些关于NewModules. 这是正常的吗?而且,是否可以将所有历史信息传输到 Develop 分支?

Thank you!!

谢谢!!

采纳答案by ralphtheninja

Am I doing a propery use of git?

我在正确使用 git 吗?

Yes the workflow that you describe is pretty much standard workflow. You create some branch, you work on it and when you're done you merge it and remove the not needed branch (unless you are going to continue developing on that branch).

是的,您描述的工作流程几乎是标准工作流程。您创建了一些分支,然后对其进行处理,完成后合并它并删除不需要的分支(除非您打算继续在该分支上进行开发)。

After removing a branch, viewing the history it seems to me that I have lost every information about the branch itself... is that normal?

删除分支后,查看历史记录,在我看来,我丢失了有关分支本身的所有信息……这正常吗?

Yes this is normal.

是的,这是正常的。

is it possible to remove a branch but leaving the history information unbroken?

是否可以删除一个分支,但不破坏历史信息?

Not sure what you mean here. As long as you have merged the branch before deleting it, the history is still there. You just merged it into another branch and the history can be seen on that branch. There is no way to know when a branch was deleted if that's what you are asking for.

不确定你在这里的意思。只要您在删除之前合并了分支,历史记录仍然存在。您刚刚将其合并到另一个分支,并且可以在该分支上看到历史记录。如果这是您的要求,则无法知道何时删除了分支。

回答by Simon Featherstone

I suggest you read http://nvie.com/posts/a-successful-git-branching-model/which defines a good pattern for git branching.

我建议你阅读http://nvie.com/posts/a-successful-git-branching-model/,它定义了一个很好的 git 分支模式。

I have found that I keep development branches for a period, until time renders the change history you made in those revisions not worth keeping (about 6 months), and then delete them.

我发现我将开发分支保留了一段时间,直到时间使您在这些修订中所做的更改历史记录不值得保留(大约 6 个月),然后将其删除。