DEV Community

How do you prepare your commits?

Sandor Dargo on July 03, 2019

What is your process to create a new commit? Is it just git commit -am? Or is it more sophisticated? Mine used to be something like this: for ...
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!

Collapse
 
loki profile image
Loki Le DEV

I just use git-gui, it shows the diff, you can select lines or the whole file you can amend easily.

I just type gg to launch it and we're good to go.

Collapse
 
sandordargo profile image
Sandor Dargo

You don't have gui available to everywhere. Plus in most cases you can be faster through the CLI if you know how to use it.

Collapse
 
loki profile image
Loki Le DEV • Edited

At my work I have access to a graphical environment almost all the time. For other tools I agree with you that CLI can be faster (if you spend time configuring and learning it) but for git I've found that git-gui does just the right job for me without needing for customization.

Now that I think of it I haven't typed git commit in years ^

Collapse
 
mrxinu profile image
Steve Klassen (They/Them)

That's pretty slick. 🤘