Prefer git reset to undoing for removing false starts

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the workflows category.

Last Updated: 2024-11-21

In Project S, I wanted to extract the address fields to a separate model. I did this as below.

<? php
// before
protected $fillable = [
    'gender', 'title', 'first_name', 'last_name',
    'company_name', 
    'street', 'house_number', 'postalcode', 
    'url', 'email',
    'phone', 'fax', 'district',
  ]

// after: street, house_number etc. removed
protected $fillable = [
    'gender', 'title', 'first_name', 'last_name',
    'company_name', 
    'url', 'email',
    'phone', 'fax', 'district',
  ]

Having a change of heart, I hit undo on my editor. When I ran my tests, which worked before the change back and forth, I got weird failures... the data was not set on the Advisor.

The real issue was I had not undone all the changes. I should have used git to guarantee this.

Lessons

  1. If you change your mind about a destructive refactor or change, use git reset --hard instead of the more risky "undo in editor" strategy. (Proviso: remember to reseed local DB if you migrated)
  2. To enable the possibility of using git, start off the refactor on a clean commit or branch, such that nothing worthwhile will be lost if a reset happens.
  3. Always proof read changes before committing. Yes it is boring, but you save time in the long run. This bug took me an hour and sapped my energy hard.