Git Pills: commit only some parts of a changed file

While versioning a source code file, one of the common (and worst) mistake you can do is adding changes made for different purposes in a single commit.

Be effective in this task is not easy at all; I’m working day by day trying to avoid this subtle trap, but I’m far to be perfect (it implies writing good, small and maintainable source code files, you know… :) ).
If even you want to improve, in my Git Essentials book I suggest some of the more effective tricks I found. Unfortunately I forgot to mention the last hope you have, so I will amend here.

Git provides an appropriate command to include, while adding, only some of the modifications you made in a file:

When you fire this command, you enter in the “interactive mode“; Git shows you the changes you made to the file after the previous add, then allows you to include them (Git calls them “hunks”) with some different modalities (see git add man page for details, or type “?” when Git asks you about hunks to include):

The important thing to remember here is to always add the file you modify when you take a step ahead in your feature implementation.

Happy versioning :)

Nando :)

Leave a Reply

Your email address will not be published. Required fields are marked *