Search code examples
gitcommentsdocumentationcode-documentation

Is it possible to make a git diff that results in no changes?


So this is probably a silly idea, and there are probably workarounds/other solutions to this. I am curious about these, but also curious about the specific answer to the following question.

Is it possible to create a diff for a commit that simply removes and adds the same exact code thereby giving you an opportunity to create a commit that is really just metadata to specific lines of code, i.e. way to create a comment that is ephemerally attached to the lines of code until those lines are changed?

Reason: My git reflexes make me want to ensure that every commit is fully working code, that way you don't have to guess how many commits you need to pop off if you need to roll something back. However this essentially gives you a single commit message to apply to all lines of code changed in that commit. (Maybe this is my fundamental problem?)

If there was a way to create a non-diff, that I could create a commit on, I could simply add a "comment" (commit message) to a few lines of code, embedded in which would be the author of the "comment", and a date-stamp of when the "comment" was made.

Then I could just get rid of all hard comments (i.e. lines starting with // or # or whatever) in my code, and look at a blame if I want to look for comments. And if I ever change something I either have to intentionally perpetuate the comment, write a new one, or just leave it up to whatever functional change I made. In this way you would never have a TODO that doesn't need to be done, and you would never have a NOTE that doesn't apply anymore once you've changed the code.


Solution

  • Is it possible to create a diff for a commit that simply removes and adds the same exact code thereby giving you an opportunity to create a commit that is really just metadata to specific lines of code, i.e. way to create a comment that is ephemerally attached to the lines of code until those lines are changed?

    Simply, No. Your example of a comment would be a code change and thus it would show up in the diff.

    Some potential options:

    1. You could create an empty commit as described in SebDieBln's answer, which has no changes and only a commit message, in which you could say whatever you'd like.
    2. You could create 2 commits, where the second one undoes the first. One way to accomplish this is to make some changes in 1 commit, and then revert that commit which will make a 2nd commit. In this case, the diff between the parent of the first commit and the second commit will have no changes, but the diff between the parent of the first commit and the first commit will have your changes you wish to capture and possibly do something with.