I’ve been using git and more generally version control for a while but always struggled for a use case: you start working on something very specific but you end up doing several changes more or less related to the original goal. Three very common usages are
- the boy scout rule: when spotting something I don’t like like variable name or outdated documentation I usually fix it right away, but I don’t want it to appear under the same label as the original change, I want a dedicated commit
- debugging: I usually add some tracing calls to see what happens locally, for example when working on a Flux app my dispatcher is always printing actions on my developer console, but this should never go to the repository
- non atomic changes: functionally modifying something usually technically impacts several aspects, but after finishing the work I usually realize how I could have split the work in smaller tasks after which the build would pass, making each individual commit more understandable
Another thing I usually do is to review any change I am going to commit which requires repeatedly calling
git status and
git diff [file].
For all this cases I have a large change set in the working repository and I want to stage part of them.
git add [file] allows to stage only some of the files, which is already nice.
git stash allows to reset the working directory, work and commit some changes, and then come back to the original task, pretty neat for applying the boy scout rule.
But often I need to include only a subset of changes in a given file, and for that
git add --patch (doc) is really nice. This command will prompt for each identified diff, which to stage or to ignore. So what I now do is
git add . --patch and git asks me for each deleted file if I want to remove it and for each difference in each modified file if I want to include the change: I just need to hit
n and it moves to the next difference. That also solves the reviewing issue, I am prompted for each difference and choose whether to include it right away. I also tried interactive mode (
git add -i) but always went to patch mode, so better skip the first screen.
With this workflow I spend less time on git operations and feel safer by including the exact changes I want, without compromising on commit size.