Imagine this scenario:
You’re working on a piece of code. Task A is on top of you priority list, so you start working on that, change a few lines of code. As you’re working on A, you think “Oh! There’s this thing B I could fix. That’ll just take a minute !”.
So you go and do B, which makes you think of C. You finish C before going back to A. Then skip to D. Alternate a bit between working on B and E. Get back to A again. Start doing Z.
After a while, you finally get to the end of A. You proudly type git commit -am”
before realizing all these changes you made can’t be described in a single one-line comment. These changes may or may not be all in the same file, but if you want to describe everything you changed in a single commit, there’s going to be a long list to type out. And that’s not a very good practice.
But fear not! Git has you covered. Simply use the -p
(or --patch
) option. This option makes it possible to only commit lines that are relevant to a certain change. Git will event separate your modified lines into convenient blocs that you can selectively add (or not) to the current commit.
So for instance, you might start with git commit -apm"Task A”
and go through all your modifications to select those that are relevant to A. Then do the same thing with B,… Now instead of having a blob of a commit with a gazillion things going on inside, you can have nice unitary commits and a usable repository.
How do you manage your brain jumping unprompted from one task to another? Come talk with me on Mastodon.