简体   繁体   English

在git中,为什么要在分支而不是主分支上工作?

[英]In git, why should one work over a branch instead of the main branch?

I keep reading about how one should always work on a different branch than the main one. 我一直在阅读有关应该如何总是在与主要分支不同的分支上工作的内容。 As soon as I get the job done, I merge them. 一旦我完成工作,我就合并它们。 Why is this any better than simply working on the main branch? 为什么这比简单地在主分支上工作更好?

The only advantage I see at first glance is that then there is a somewhat safe way of always knowing what is the "last known working version" -- the main branch. 我第一眼看到的唯一优势是,有一种安全的方式可以随时了解什么是“最后已知的工作版本” - 主要分支。 Is that the reason? 这是什么原因?

The primary advantage of working in a branch is that you can make commits for an isolated feature while still being able to make fixes on master. 在分支机构中工作的主要优点是,您可以为隔离功能提交提交,同时仍然可以在主服务器上进行修复。 You can also do things like "squash" commits with rebase -i if you feel that multiple commits should actually show up as a single commit to other users. 如果您认为多个提交实际上应该显示为对其他用户的单个提交,您还可以使用rebase -i执行“squash”提交等操作。

You can also work on multiple experimental features at the same time. 您还可以同时处理多个实验性功能。 You may later decide to scrap that feature or implement it in another way and you can just delete that branch without cluttering your master history. 您可能稍后决定废弃该功能或以其他方式实现它,您只需删除该分支而不会混淆您的主历史记录。

I often have a handful of experimental feature branches in any given project. 在任何给定的项目中,我经常会有一些实验性功能分支。 Even for just quickly jotting down some thoughts in the form of code they are very useful. 即使只是快速记下代码形式的一些想法,它们也非常有用。

Here's one example that happens a lot for me: 这是一个对我来说很有用的例子:

Let's say you are working on a feature, its an extensive one, so its going to take many commits. 假设您正在开发一个功能,它是一个广泛的功能,所以它将需要许多提交。 You are about half way done, so the code isn't stable, but it's coming along nicely. 你大约完成了一半,所以代码不稳定,但它很顺利。

Now, a critical bug is found in the production version of the code, (your master branch, presumably). 现在,在代码的生产版本中发现了一个关键错误(大概是你的主分支)。 If you have been committing against the master branch, how do you fix it and push out the fix without pushing out the broken unfinished code? 如果你一直在反对主分支,你如何解决它并推出修复而不推出破碎的未完成的代码?

If you had been working on your new feature in a branch that isn't the master, you can easily just switch to the master branch, fix the bug, and push out the code. 如果您在不是主服务器的分支中处理新功能,则可以轻松切换到主分支,修复错误并推出代码。 Then switch back to your feature branch and continue working. 然后切换回功能分支并继续工作。

The ability to do that alone is enough for me to make a branch anytime im working on something. 单独完成这项工作的能力足以让我在任何时候制作分支。

The other answers have made some good points, but there's another big one: what if you publish work to a central repository, which others also access? 其他答案已经提出了一些好处,但还有另一个重点:如果您将工作发布到中央存储库,其他人也可以访问? Perhaps by pushing, perhaps by pull requests, but the upshot is that things that you do get out there. 也许是通过推动,或许是通过拉动请求,但结果就是你所做的事情。 Sticking to the convention of publishing work only from your master branch really helps. 仅仅从主分支坚持出版工作的惯例确实有帮助。

As you said, you can think of master as "the last known working version", but you can also think of it as "my newest stable version". 正如你所说,你可以把master想象成“最后一个已知的工作版本”,但你也可以把它想象成“我最新的稳定版本”。 If it's the only one you publish from, then you know that you can't ever do anything crazy to it, but also that you can do those crazy things to any other branch. 如果它是你发布的唯一一个,那么你知道你不能做任何疯狂的事情,而且你可以做任何其他分支的疯狂的事情。 You have free reign to amend commits, squash them, rebase branches around, all of those ways that Git provide to fix the inevitable oversights we make while developing. 你可以自由地修改提交,压缩它们,重新分支,以及Git提供的所有方式来解决我们在开发过程中不可避免的疏忽。 And you never have to think, "hm, did I push that work already?" 你永远不必思考,“嗯,我已经推动了这项工作吗?” - you haven't, since it's not on your master branch yet. - 你还没有,因为它还没有在你的主分支上。 You can also try anything, coding-wise - hack away, commit partially finished work, move around between ideas, whatever you like - and be confident you'll never accidentally show it to anyone else until you say "I'm finished" and merge it to master. 你也可以尝试任何东西,编码方式 - 破解,提交部分完成的工作,在想法之间移动,无论你喜欢什么 - 并且相信你永远不会意外地向其他人展示它,直到你说“我已经完成”和将它合并到主人。

The key part here is the notion of publishing your work. 这里的关键部分是发布你的作品的概念。 If this were your own private repository, if you realized your master branch was broken in some way, it'd only inconvenience you. 如果这是您自己的私人存储库,如果您意识到您的主分支在某种程度上被破坏了,那只会给您带来不便。 But as soon as other people are involved, you could be messing with them too. 但是一旦其他人参与进来,你也可能会搞砸他们。

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

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