I'm currently working on someone else's code, and I'd like to add some comments for my own understanding. Is there a way to add comments directly in Git that won't be included in the commits? I know that I can use an ignore file to prevent entire files from being committed, but can I prevent specific lines from being committed? If that's not feasible, do you have suggestions for other methods to keep my comments private but accessible?
5 Answers
Have you tried using extensions that let you bookmark lines in files? These tools let you write notes attached to those bookmarks, so you can keep the comments private without messing with the actual code.
It would be great to have a feature for personal comments, but there are challenges. If the code gets revised after you add your notes, you'd have to ensure that your comments still make sense, which can be tricky. A better method might be to keep a separate document where you jot down your thoughts about the code, including snippets or screenshots. This way, you won't clutter the actual code with personal notes, and should the code change, you can easily adjust your notes without worrying about inconsistencies.
No, Git doesn't allow ignoring specific lines within a file—it can only ignore entire files. Since Git is a collaborative tool, it assumes that multiple people will work on the same codebase, so adding personal comments in shared files isn't really what it's designed for. If your notes help you understand the code, why not share them? It could assist others as well.
A simple solution would be to create a local branch off of your main or feature branch for your personal notes. You can keep your commented branch for your own use and have a clean version for pushing.
Unfortunately, you can’t easily add personal comments in Git while keeping the core functions intact. One workaround might be to create a separate branch to add your comments there, but just be prepared for potential merge conflicts later on.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically