When we commit the respective code into our VCS, we do not write another lengthy explanation, but instead just add the URL to the code on Stack Overflow available under the "share" link of that question or answer.
If you have an interest in preserving source information, this is a mistake, or rather a risk that you are taking. If information that you didn't copy into the codebase itself is truly that important, you should at least provide links to multiple sources, or preferably back them up yourselves (e.g., link to a Stack Overflow page and an Archive.org version of said page, made at the time you used/copied the code).
In the past, this has seemed practical, specifically since SO explains in their legal terms that we give up all our rights on our writings here, in order for SO to protect the documentation.
Not sure exactly what this is trying to say, but it's almost certainly an inaccurate representation. When you post on Stack Overflow, you don't "give up all your rights" to your content; you license it to Stack Overflow for them to use however they like, more or less, under the Creative Commons license or the MIT license (IIRC there's a slightly different license for prose vs code, or at least I recall reading about that once over on an MSE Q&A...). You still retain your own rights and can license it under a more permissive license than the one you assigned to Stack Overflow, if you wish.
But when reading some old commit logs, we now find that, ten years later these links into SO return a "page not found" reply or the message "removed for reasons of moderation".
Yep, that's unfortunate. But, hopefully this is your wake-up call to stop relying on this unreliable method for documentation. Unless you are paying Stack Overflow directly, you have no guarantee or recourse to follow to ensure information they are providing for you will remain available. See my response above for better methods to follow from here on out.
Sadly, we learn about such consequences only when it is too late. Now, about solving the problem:
- getting back the contents may or may not be possible, as internet archives may or may not have archived them. Are there any other services that document what gets deleted over time by Stack Overflow?
- then fixing a dead link in a ten-year-old VCS appears to be a difficult task. What are the best strategies to achieve that?
Well, no, not really. Archive.org versions may well exist of a given page, especially if it is/was popular. If an entire question is deleted, it was probably off-topic here, so if it was popular it probably already exists on a proper sister site on the network like Server Fault or Database Administrators (or it was not really of any value for things like a codebase).
However, anyone can see deleted content if they have 10,000+ reputation on a site. So... get to work! Alternatively, you can download the Stack Overflow data dumps which contain all information, including deleted content as far as I know, on the site. If they don't include deleted content, you can download a few different data dumps from just before the time when the link was added to your source control, and query the information from the data dump.
- and finally, how can we in the future effectively warn people to protect themselves from such dangers, such as to not use Stack Overflow, specifically for documentation purposes?
I mean, this very Q&A is a good warning, I think. But, there's no need to warn people not to use Stack Overflow, because that wasn't your problem; your problem was thinking that all content on Stack Overflow would remain visible to everyone, forever. That's never been the case, and Stack Overflow has never made that promise anywhere. So really you can 'warn' people by encouraging them to read the site rules and Help Center before they start to use or rely on the site. That way, they don't experience the shock you did.
As for the one link you did provide (in the comments), https://stackoverflow.com/questions/49366079/postgresql-howto-avoid-multiple-execution-of-subquery-containing-stable-functio, that is an odd thing to write this post about. Not only is it a question that doesn't have any answers or comments (so what's the point of including it in your code base or source control?), but it's also your own question, which you can still see, as long as you are logged in, and you can access it from your questions list on your profile: https://stackoverflow.com/users/6201427/pmc?tab=questions (at the bottom) or directly via https://stackoverflow.com/users/deleted-questions/6201427 (again only while you are logged in, since it is your own question).
One thing you can also do is take a full-page screenshot of any content you want to link to, save that as an image, and then upload that image to your source control environment as well. This way you see exactly what was visible, in the same state, when the resource was originally preserved, no matter how far into the future you might need to look at it.