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)