DEV Community

Cover image for 🔄 Automate Laravel Migrations After - git pull — A Simple Local Setup for Teams
Tahsin Abrar
Tahsin Abrar

Posted on • Edited on

🔄 Automate Laravel Migrations After - git pull — A Simple Local Setup for Teams

Wait, why is my feature breaking? Oh no… I forgot to run php artisan migrate after pulling!

If you're part of a Laravel development team, this scenario might sound familiar. It happened to me more than once, and maybe it’s happening to you too. In this post, I’ll walk you through a simple, local-only solution to automatically run Laravel migrations right after you run git pull. No CI/CD, no shell scripts — just a smart git alias.


🧩 The Problem — A Common Developer Story

In our team, we often push updates that include database migrations. For example:

  1. I work on a feature and create a new migration.
  2. After testing, I push it to GitHub.
  3. My teammate pulls the latest code for code review.
  4. But — they forget to run php artisan migrate 😵‍💫.
  5. Boom. The app crashes because the schema isn’t up to date.

This isn’t anyone’s fault — it’s easy to forget when you’re multitasking or context-switching. That’s why I wanted a local automation that ensures every time I do a git pull, it also runs migrations and refreshes Laravel’s config cache.

So here's how we fixed it... 👇


⚙️ The Solution — Custom Git Alias with Migration Command

We're going to define a custom git alias that:

  • Pulls the latest changes
  • Installs dependencies
  • Runs the Laravel migrations
  • Clears and rebuilds the config cache

✅ Final Alias Command:

alias gpm='git pull && composer install && php artisan migrate --force && php artisan config:cache'
Enter fullscreen mode Exit fullscreen mode

✅ Simpler Version (without Composer install/cache):

[alias]
    gpm = "!git pull && php artisan migrate && php artisan cache:clear"
Enter fullscreen mode Exit fullscreen mode

You can choose based on your needs.


🧑‍💻 Step-by-Step Setup

Step 1: Set VS Code as Your Default Git Editor (Optional)

git config --global core.editor "code --wait"
Enter fullscreen mode Exit fullscreen mode

This makes editing the Git config easier.


Step 2: Open Your Global Git Config

git config --global --edit
Enter fullscreen mode Exit fullscreen mode

This opens the Git configuration file. You’ll be adding a custom alias.


Step 3: Add the Alias

Inside the opened file, scroll to the bottom and add:

[alias]
    gpm = "!git pull && php artisan migrate && php artisan cache:clear"
Enter fullscreen mode Exit fullscreen mode

Or for a more complete workflow:

[alias]
    gpm = "!git pull && composer install && php artisan migrate --force && php artisan config:cache"
Enter fullscreen mode Exit fullscreen mode

🔐 --force is important in Laravel when running migrate from a script/alias to bypass confirmation prompts.


Step 4: Save and Close

Save the file and close the editor.


Step 5: Use Your New Command

Now whenever you want to pull the latest changes and run migrations:

git gpm
Enter fullscreen mode Exit fullscreen mode

✅ It will:

  • Pull the latest changes
  • Run Laravel migrations
  • Clear the cache

No more forgetting php artisan migrate!

Top comments (0)