• How To Politely Ask Git To Remind You About Migrations

    Two dark guide rails merge at a pale stone tile that activates a brass reminder bell

    Switch branches, pick up a new database migration, forget to run it. Let Git give us a nudge.

    The post-checkout hook receives the previous HEAD, the new HEAD, and a flag: 1 for a branch checkout, 0 for a file checkout. Those commit IDs give us the comparison directly, without guessing from the reflog.

    This Bash hook lists added migration files in green and modified ones in red. Set migration_dir to your project’s migration directory, relative to the repository root; db/migrate is the example here.

    bash
    #!/bin/bash
    
    old_head=$1
    new_head=$2
    migration_dir='db/migrate'
    
    # Skip file checkouts, unchanged HEADs, and the initial checkout of a clone.
    if [ "$3" != 1 ] || [ "$old_head" = "$new_head" ] || [[ "$old_head" =~ ^0+$ ]]; then
      exit 0
    fi
    
    report_migrations() {
      local filter=$1 label=$2 color=$3 filename printed=0
    
      git diff --no-ext-diff --no-renames --name-only -z --diff-filter="$filter" \
        "$old_head" "$new_head" -- "$migration_dir/" |
        while IFS= read -r -d '' filename; do
          if [ "$printed" -eq 0 ]; then
            printf '\033[%sm%s:\033[0m\n' "$color" "$label"
            printed=1
          fi
    
          printf '\033[%sm  %s\033[0m\n' "$color" "$filename"
        done
    }
    
    report_migrations A 'Migrations added' 32
    report_migrations M 'Migrations modified' 31
    
    exit 0

    The git diff options select added (A) and modified (M) files. -z and read -r -d '' keep spaces, backslashes and newlines in filenames intact. Rename detection is disabled, so a renamed file is treated as a deletion and an addition; deletions are not listed.

    Install the hook

    Save the script as .git/hooks/post-checkout in a normal clone and make it executable:

    bash
    chmod +x .git/hooks/post-checkout

    If you already have a post-checkout hook, integrate the reminder into it rather than replacing it. If your repository uses core.hooksPath, put the script in that configured directory instead. Git runs this hook from the working tree root, so migration_dir stays relative to the project.

    What the reminder covers

    Switch to a branch with an added or modified file under the chosen directory and the hook prints its name. A checkout of an individual file, an unchanged HEAD, or an initial checkout with no previous commit produces no reminder. A detached-HEAD checkout can also produce a list: Git uses the same flag for that operation.

    This recipe covers checkout and branch switching. It does not provide migration reminders for git pull, merges or rebases; do not install the same script as post-merge or post-rewrite, whose inputs differ.

    The list compares Git trees, not your database. A file added on the destination branch may already have been applied locally. Use your framework’s migration tracking to decide what still needs to run; the hook just supplies the nudge.