Git 提交风格:一次更改所有文件还是一次更改一个文件?

发布于 2024-12-04 13:11:14 字数 201 浏览 0 评论 0 原文

我在晚上通过一次提交来保存许多文件的工作。我想知道为每个文件提交是否会更好,但这似乎需要更多工作。

我对现在的情况没有任何问题,但我计划将我的代码放在 GitHub 上,并且我希望它易于理解。

我想知道其他使用 git 的人在做什么。另外,如果你能为我拼写出来的话。我是 Git 新手,一直在 Windows 中使用 TortoiseGit 和 gitk。

I am saving my work at night with a single commit for many files. I wonder if it would be better to commit for each file but this seems like a lot more work.

I have no problem with the way things are now but I plan to put my code on GitHub and I want it to be easy to understand.

I'm wondering what the rest of you who use git are doing. Also if you could kind of spell it out for me. I'm new to Git and I've been using TortoiseGit and gitk in Windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惟欲睡 2024-12-11 13:11:14

何时承诺以及承诺什么是一门艺术,没有非黑即白的规则。话虽如此,有些习惯比其他习惯更容易理解。

一般来说,我认为你应该优化你的提交以使其易于理解 - 如果你返回并阅读提交的差异,你能弄清楚你在更改中完成了什么吗?

如果你想更具体,这里有一个长长的列表,列出了我认为应该做和不应该做的事情:

  • 不要在每一个小小的更改后都提交 - 每行更改,每个文件更改等。
  • 不要为整个工作工作并在一天结束时做出一项巨大的承诺。
  • 区分不同功能的提交 - 例如开发功能 foo 与修复 bug #2。
  • 为移动/重命名文件进行单独的提交,因为 Git 通过这种方式更容易跟踪。
  • 请考虑针对可恢复性进行优化:如果您不喜欢所做的更改,即使在新的更改堆积在上面之后,是否也可以轻松撤消它?

When to commit and what to commit is an art, and there are no black-and-white rules. That being said, there are habits that are easier to understand than others.

In general, I think you should optimize your commits for understandability - if you go back and read the diff for the commit, can you figure out what you accomplished in the changes?

If you want to be more specific, here's a long list of what I think are do's and don'ts:

  • Don't commit after every single little change - every line changed, every file changed, etc.
  • Don't work for an entire day and make one gigantic commit at the end of the day.
  • Do separate out commits for different features - e.g. developing feature foo vs. fixing bug #2.
  • Do a separate commit for moving/renaming files, because it's easier for Git to track this way.
  • Do think about optimizing for revertability: If you dislike a change that you made, is it easy to undo it even after new changes have been piled on top?
甜尕妞 2024-12-11 13:11:14

“易于理解”还意味着:

  • 提交不仅代表“检查点”(就像在每次文件修改后提交一样),而且还代表
  • 易于理解的代码的连贯状态 git bisect (即每次提交应代表任务中的更改,它编译并添加一个演变或新功能,而不是“检查点提交”,这会使 git bisect 很快失败)

请参阅“了解 Git 工作流程" 了解更多信息:您需要区分:

  • 私有分支(您从不推送),您基本上可以在任何地方提交时间和
  • 公共分支(您将在 GitHub 上推送),需要清理并进行有意义的提交。

因此请注意“快进” Git 默认使用的合并:不要忘记清理要以这种方式合并到公共分支的分支的历史记录。

"Easy to understand" means also:

  • commits representing not just "checkpoint" (like they would if you committed after each file modification), but a coherent state of the code
  • easy to git bisect (ie each commit should represent a change in a task, which compiles and add an evolution or a new feature, and not a "checkpoint commit", which would make the git bisect fails way too soon)

See "understanding the Git workflow" for more: you need to differentiate:

  • private branches (that you never push), where you can commit basically at any time, and
  • public branches (that you will push on GitHub), which needs to be clean-up and have meaningful commits.

So pay attention to the "fast-forward" merge that Git uses by default: don't forget to clean-up the history of branches you are about to merge that way into public branches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文