git 我应该什么时候创建一个新分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15633409/
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
When should i create a new branch?
提问by Moonlit
I am using git as the first time for a versioning system. I am starting a new project, and therefore to experiment a little bit with the technologies used in the project (hello world examples...) i want to create something like a "playground" branch. Is it common to create a new branch "playground" or should i just create a folder named playground in the master branch?
我第一次使用 git 作为版本控制系统。我正在开始一个新项目,因此要对项目中使用的技术进行一些试验(hello world 示例...),我想创建一个类似于“游乐场”分支的东西。创建一个新分支“playground”是否很常见,还是应该在主分支中创建一个名为 playground 的文件夹?
regards
问候
回答by Kevin Bowersox
You should create a new branch when you're doing development work that is somewhat experimental in nature. So in your scenario definitely create a new branch and not a folder within master. If you created your sandbox work as a directory in the master, it's going to reside there until you remove it using git. Having dead code sitting in the master branch is not ideal by any means, since it can confuse other developers and may just sit there rotting for the lifetime of the application.
当您进行本质上有点实验性的开发工作时,您应该创建一个新分支。因此,在您的场景中,一定要创建一个新分支,而不是 master 中的文件夹。如果您将沙箱工作创建为 master 中的目录,它将一直驻留在那里,直到您使用 git 将其删除。将死代码放在 master 分支中无论如何都不理想,因为它会使其他开发人员感到困惑,并且可能在应用程序的整个生命周期中都坐在那里腐烂。
If your team were to experience a bug, you wouldn't want them to waste time exploring the experimental work to determine if the bug existed within that directory. Creating a new branch allows you to isolate your changes from the master branch. If your experimentation goes well you always have the option to merge your changes into the master branch. If things don't go so well you can always discard the branch or keep it within your local repository.
如果您的团队遇到错误,您不会希望他们浪费时间探索实验工作以确定该目录中是否存在错误。创建新分支允许您将更改与主分支隔离。如果您的实验进展顺利,您始终可以选择将您的更改合并到主分支中。如果事情进展不顺利,您可以随时丢弃该分支或将其保留在本地存储库中。
回答by Ikke
Branches have many uses, and it depends a bit on your workflow. Two commonly used workflows are:
分支有很多用途,这在一定程度上取决于您的工作流程。两个常用的工作流程是:
Both use so called topic branches to build new features in, which get merged back once ready / accepted.
两者都使用所谓的主题分支来构建新功能,一旦准备好/被接受,它们就会合并回来。
Github flow is fairly simple, and is obviously what github uses. Gitflow is a bit more complex, and is more suited when you need to support several versions of an app, where hotfixes can be applied.
Github流程相当简单,显然是github使用的。Gitflow 稍微复杂一些,更适合需要支持多个版本的应用程序,其中可以应用修补程序。
In the end, it's a matter of preference what kind of workflow you use, but because creating branches is very cheap in git, it doesn't really matter how many branches you create (and eventually, delete again).
最后,你使用什么样的工作流程是一个偏好问题,但是因为在 git 中创建分支非常便宜,所以创建多少个分支并不重要(最终,再次删除)。
回答by sachinjain024
You should consider creating new branches and work on them in these cases:-
在这些情况下,您应该考虑创建新分支并对其进行处理:-
When you want to work/test out something in a sandbox environment.
It is good practice to keep your commits short in nature So frequent commits from you might disturb others' development area So its better to complete your work on a branch and then later merge your branch into main branch. [Tip]Remember to keep your branch in sync with main branch by merging main branch into your branch frequently. So at later point of time, you don't have a lot of stuff to merge by hand.
You want to solve a bug. Its better to solve it on other branch and merge it later.
If your commit goes wrong/broke your build, the production build is not affected. So I prefer to use atleast two branches dev branch and prod branch. When everything is fully tested, merge your dev branch to production branch.
当您想在沙盒环境中工作/测试某些东西时。
保持你的提交本质上是一种很好的做法 如此频繁的提交可能会干扰其他人的开发区域 所以最好在一个分支上完成你的工作,然后将你的分支合并到主分支。[提示]记得经常将主分支合并到你的分支中,以保持你的分支与主分支同步。所以在以后的时间点,你没有很多东西要手动合并。
你想解决一个错误。最好在其他分支上解决它,然后再合并。
如果您的提交出错/破坏了您的构建,则生产构建不受影响。所以我更喜欢使用至少两个分支 dev 分支和 prod 分支。当一切都经过完全测试后,将您的开发分支合并到生产分支。
回答by rjocoleman
You should defiantly learn by experimenting but in a playground repo.
A playground directory in a repo will be will less benefit.
Have a play, make some mistakes learn some things - delete it a few times and go wild.
你应该勇敢地通过实验来学习,但是在操场上。
回购中的操场目录将不会有什么好处。
玩一玩,犯一些错误,学习一些东西 - 删除几次然后发疯。
e.g.
例如
$ mkdir playground
$ git init
$ touch hello-world
$ git add hello-world
$ git commit -m "my first commit"
$ git branch goodbye
$ git checkout goodbye
$ echo "goodbye" | cat >>hello-world
$ git status
$ git add hello-world
$ git commit -m "goodbye commit"
$ git merge master
I'd also reccomend grabbing a fork of an active project in the language of your choice from GitHub and give a go with merging, rebasing etc there with real code.
我还建议您从 GitHub 中以您选择的语言抓取一个活跃项目的分支,并尝试在那里使用真实代码进行合并、重新定位等。
回答by René H?hle
The common way is to use the master branch as live branch. Then you make new branches from the master and work on them (feature branches). After you have finished your work you merge the changes back to your master.
常见的方式是使用 master 分支作为 live 分支。然后你从 master 创建新的分支并处理它们(功能分支)。完成工作后,您将更改合并回您的母版。
You can build new remote branches like a dev branch and merge your changes to this branch when you want only a preview. There are some methods but there are a lot of informations on google.
您可以像开发分支一样构建新的远程分支,并在您只需要预览时将更改合并到该分支。有一些方法,但谷歌上有很多信息。