在Git中拥有尽可能多的提交还是越少越好? [英] Is it better to have as many commits as possible in Git or as few as possible?

查看:73
本文介绍了在Git中拥有尽可能多的提交还是越少越好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近与一位同事进行了辩论,他坚持认为,由于合并冲突,尽可能少的提交会更好.我认为通过使用尽可能多的提交,您拥有的细节越多越好.

I recently had a debate with a colleague and he is adamant that having as few commits as possible is better, due to merge conflicts. I argue that the more detail you have by using as many commits as possible, the better.

拥有更多或更少的提交会更好,为什么?

Is it better to have more commits, or less, and why?

推荐答案

如果您的同事由于某种原因导致更多合并冲突而阻止了许多提交,那是不正确的,仅提交次数与合并冲突无关.如果您的代码在将一个分支合并到另一个分支时将发生冲突,那么该分支具有1个提交还是1,000个提交都无所谓,这两种方式都会引起冲突.

If your colleague discourages many commits because that somehow causes more merge conflicts, then that is incorrect, the number of commits alone has nothing to do with merge conflicts. If your code is going to conflict when you merge a branch into another, it won't matter if that branch has 1 commit or 1,000, it's going to cause a conflict either way.

这是因为,通常,Git仅比较每个分支上最终提交时的代码状态 ,以及它们之间的最佳"共同祖先(由合并算法确定).在祖先和分支的尖端提交之间的任何中间提交都将不被考虑.

This is because, in general, Git only compares the state of the code at the final commits on each branch, along with the "best" common ancestor between them (as determined by the merge algorithm). Any intermediary commits between the ancestor and the tip commit of a branch won't even be considered.

也就是说,当您与其他人共享历史记录时,您可能需要在使提交内容足够详细以使其有用的同时又要使其稀疏以呈现清晰易懂的历史记录之间取得平衡.有时,您的分支机构需要的全部是1次提交,以清楚显示更改的历史记录,而有时您可能需要更多.是否以一种或另一种方式进行操作取决于情况.

That said, when you share your history with other people, you might want to strike a balance between having your commits detailed enough to be useful, but also sparse enough to present a clear, easily understood history. Sometimes all your branch needs is 1 commit to clearly show a history of changes, and other times you might need more. Whether to do it one way or another is situational.

如果您只是私下处理分支,则可以根据需要进行任意多次提交,因为在与他人共享更改之前,您始终可以重写历史记录,以便以后变得更短,更清晰.

If you're just working on a branch privately, you can commit as often as you want, however you want, because you can always rewrite your history to be shorter and clearer later, before you share your changes with other people.

这篇关于在Git中拥有尽可能多的提交还是越少越好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆