DEV Community

Discussion on: How do you prepare your commits?

Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler

I first do a git diff to review the changes I made and decide what I’m going to commit first if several commits are desired.

Then I use git add -p. It’s a bit daunting at first, but you get used to it. It walks you through all the changes (as patch hunks) and you can decide whether to add (y) it or not (n). Then you have extra commands for more control, like add the whole file (a) or skip it (d), split the current hunk into smaller parts (s), and even edit the patch hunk in your editor (e).

Since I’ve been doing this, my commits are much more atomic, because I don’t limit myself to committing whole files at a time. It’s also a great safeguard, because you actively look at anything you’re about to commit, and are less likely to commit debug statements, for example. You will still forget to add new files, though, I can guarantee it. ;-) (speaking of new files, you can add them with git add -N my_new_file to record the addition of the file without its contents. Then you see the whole file in git diff and git add -p, which is sometimes useful.

The -p flag also works for reset, checkout (useful to remove debug print statements) and stash.

Editing the patches is more difficult, but it’s sometimes handy and you get used to it.

Collapse
 
tomaszprasolek profile image
Tomasz Prasołek

Instead of git add -p, I use git-istage tool. For me is more handy than git add -p.

Collapse
 
sandordargo profile image
Sandor Dargo

I didn't know about git add -N, thanks, that will be a useful tool in my belt!