On the Granularity of Git Commits

When using Git, it’s best practice to commit your code after completing a small logical step—such as implementing one or more functions that achieve a minor feature—or upon finishing a module. This approach makes code tracking and merging much easier.

Avoid committing only after writing a large amount of code. If one small logical step contains an error, rolling back your commit could mean losing significant portions of otherwise correct code.

Similarly, if you make changes to existing code or comments, like fixing a bug, you should commit those changes immediately. Don’t wait until you’ve finished writing a new feature to bundle everything into one large commit.

关于git commit代码的粒度

应该做完一个小的逻辑步骤(例如写完了一个或几个函数实现了一个小功能)就提交一次,完成一个模块就提交一次,这样方便代码回溯,也方便代码合并。不要写了大量代码后再提交一次,万一其中的一个小逻辑步骤的代码写错了,代码回滚后,大量其他正确的代码都白写了。

修改了之前的代码或注释,例如修复一个bug,也应该及时提交一次,而不是继续写完当前小功能的代码之后一起提交。