Please enable JS

Make your Git commits more specific!

Stop making blanket commits, be specific, reference a single ticket number.
March 26/Brian Young/Software

Use git add -p, be a hero!

My deployment process for our Drupal projects normally involves recreating feature based modules via drush fu module_name -y. A bad part of this is having tons of changes exported to code at one time. I try to make every git commit reference a specific issue number. This is where git add -p [filename] comes in handy.

Example scenario

Let's say I have a module that uses Features to track a custom view that lists users for my Drupal site. The customer has submitted two new tickets of feature requests. One is to change a column label (ticket #100), and the other is to sort the users by last login date (ticket #101).

I've now made the changes to the view and updated the featureized module via drush fu example_module -y.
The resulting git diff is:

I want the changes to be committed in 2 parts, one for each ticket number. I can accomplish by running:

  1. git add -p

Git will show the changes and provide us a series of options: Stage this hunk [y,n,q,a,d,/,s,e,?]. Because the changes are so close together, Git thinks it is one big hunk. We want to "split" these changes up into smaller hunks, so we'll type s at the prompt.

Git will then break the change into smaller hunks and ask what to do with each. We'll press y to stage the first hunk that is related to the label change. We'll then exclude the last hunk that is specific to the sorting change by pressing n at the prompt.

Now we can commit the staged changes.

  1. git commit -m "Changing field label. Issue: #100"

The remaining alterations are related to the sorting change we just skipped, so we can just add and commit.

  1. git add -A
  2. git commit -m "Changing sort parameters. Issue: #101"


Comments/ 0


(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
But why?

My random thoughts and documentation on the various hardware and software projects that occupy my day-to-day. My only hope is that other people can benefit from my notes.

Recent posts