Git is a great tool. Hands down, it is the best VCS on the market (well, if free and open source count as “on the market”). One of the features I really like about Git is the ability to stage and unstage files for a commit. This helps you keep your commits logically cohesive without trying to force yourself to only work on one task at a time; you can work on a few different tasks, and then put your changes into different commits by staging them appropriately.
Until recently, I thought I could only stage and commit whole files. But NO!!! You can stage and commit a portion of the changes in a file using Interactive Staging. Handy my friends!
You have a few different routes to get to the specific command we want. You can enter Interactive Staging with the command $ git add -i and choose the fifth option patch and select your file, or you can use $ git add -p <filename> (also $ git add –patch). Interactive Staging has many commands and if you have a bunch of staging business to do it can be very helpful.
$ git add -p <filename> will do fine for this post. Once you get into the patch staging prompt you are presented with hunks of changes. You can choose then to include this hunk in your staging or not. Other commands are available, but at the basic level you choose y or n for each hunk.
And there you go! You can now stage and commit portions of changes in your files. Useful I know.