Why commit messages rather than code comments?
Why commit messages rather than code comments?
That's simply not true, not in a git world where rebasing to rewrite history is standard practice.
Suppose you're making a change that has a few paragraphs of commit message explaining what you're doing and why. As well as a large well-commented change in a primary file, you've made some small incidental changes to other files. You're not going to include comments with all the context and explanation on each of these small changes.
If I'm looking at one of the files you incidentally touched later and don't understand why your change is there, I can check the git blame and have a good understanding of why the code looks like that now.
Now, why a particular piece of code came to be, why the code should have this expected behavior, why something is being added or removed or changed, may be unrelated (or unnecessary) to what this particular code is doing.
So there's two kinds of documentation here: 1) "this is what this code does", 2) "here are the series of events that led to this code". You need the former when you are editing code. You need the latter when you are reviewing code for merge, as well as in the future to figure out how the f*!$ this code came to be (which is useful to avoid re-running into a bug, troubleshooting/understanding a legacy system, or removing important code). The latter may also immediately become obsolete the second time the same block of code is edited, so we keep this historic information in e-mail or code commit messages.
1. If one needs to understand the history of the code to understand the code in its current state, the code is bad.
2. But if one is one is already familiar with the old code, it is easier to leverage that to understand the new code than start from scratch.
So we write commit messages for 2, but per 1 we can't have those commit messages be the only way to understand it, nor can we have the code comments refer to the old versions of the code that are removed[1].
[1]: except for the vaguest "other xxx you might think of were tried and didn't work".