if [[ "$VERCEL_GIT_COMMIT_REF" != "main" ]]; then exit 1; fi; git diff HEAD^ HEAD --quiet -- . && exit 0 || exit 1This command intelligently controls when Vercel builds your project in a monorepo setup:
- ✅ Always builds feature/development branches - Ensures you can test and preview all branches
- ✅ Skips unnecessary builds on main - Only builds when files in this project actually change
- ✅ Saves build time and resources - Prevents rebuilding when changes are in other parts of the monorepo
if [[ "$VERCEL_GIT_COMMIT_REF" != "main" ]]; then exit 1; fi;What this does:
- Checks if the current branch is NOT
main - If it's a feature branch (like
refactor-optimization,develop, etc.), it exits with code1 - Exit code
1= BUILD (tells Vercel to proceed with deployment)
Why this is important:
- You always want to deploy feature branches for testing and preview
- Skipping builds on feature branches would prevent you from seeing your changes
- This ensures every branch push gets a deployment URL for review
git diff HEAD^ HEAD --quiet -- . && exit 0 || exit 1What this does:
- Only runs if we didn't exit in Part 1 (i.e., we're on
mainbranch) - Compares the last commit (
HEAD) with its parent (HEAD^) - The
-- .means "only check files in the current directory" (respects Vercel's Root Directory setting) --quietmeans don't output anything, just return an exit code
Exit code logic:
- If
git difffinds NO changes: returns 0 →exit 0= SKIP BUILD - If
git difffinds changes: returns 1 →exit 1= BUILD
Why this is important:
- In a monorepo, commits might change
katachi-generator/but notpublic-site/ - This prevents rebuilding
public-sitewhen onlykatachi-generatorchanged - Saves build minutes and speeds up deployments
- The branch name of the current deployment
- Examples:
main,develop,refactor-optimization - Vercel Docs
- The command runs in the directory specified by Vercel's "Root Directory" setting
- For
public-site: Root Directory =public-site - For
mcp-server: Root Directory =mcp-server - The
.ingit diff ... -- .refers to this root directory
Vercel uses exit codes to determine build behavior:
| Exit Code | Meaning | Result |
|---|---|---|
0 |
No build needed | ⏭️ Skip deployment |
1 or greater |
Build needed | ✅ Create deployment |
Branch: refactor-optimization
Action: Push new commits
Result: ✅ BUILD (always build feature branches)
Reason: First condition exits with 1
Branch: main
Last commit: Only touched katachi-generator/
Action: Push to main
Result: ⏭️ SKIP (no changes in public-site/)
Reason: git diff finds no changes in current directory
Branch: main
Last commit: Modified public-site/components/foo.tsx
Action: Push to main
Result: ✅ BUILD (files changed in this directory)
Reason: git diff finds changes, returns 1
This command is designed for a monorepo with multiple deployable projects:
katachi-gen/
├── public-site/ ← Vercel Project 1 (Next.js app)
│ └── vercel.json
├── mcp-server/ ← Vercel Project 2 (MCP server)
│ └── vercel.json
└── katachi-generator/ ← Not deployed to Vercel
Each Vercel project uses:
- Root Directory: Set to its subdirectory (
public-siteormcp-server) - Ignored Build Step: This command (to skip unnecessary builds)
- Go to your Vercel project settings
- Navigate to Git section
- Find Ignored Build Step field
- Paste the command:
if [[ "$VERCEL_GIT_COMMIT_REF" != "main" ]]; then exit 1; fi; git diff HEAD^ HEAD --quiet -- . && exit 0 || exit 1
- Set Root Directory to your project's subdirectory (e.g.,
public-site) - Save
- ✅ Every feature branch gets a preview deployment
- ✅ Can test changes before merging to main
- ✅ Unique preview URL for each branch
- ✅ Only builds when files actually change
- ✅ Skips builds when other parts of monorepo change
- ✅ Saves Vercel build minutes
- ✅ Faster feedback loop
- ✅ Reduces unnecessary builds
- ✅ Saves resources
- ✅ Clearer deployment history (only meaningful builds)
Check: Make sure VERCEL_GIT_COMMIT_REF is set correctly
Solution: The first condition should catch all non-main branches
Check: Verify Root Directory is set correctly in Vercel
Solution: The . in git diff -- . must point to your project's directory
Check: Did you modify files in the correct directory? Solution: Git diff only checks files in the Root Directory
Note: Vercel does git clone --depth=10 (only last 10 commits)
Solution: This command works with shallow clones because it only checks HEAD^ to HEAD
exit 1- Pros: Simple, always works
- Cons: Wastes build minutes, slow feedback
git diff $VERCEL_GIT_PREVIOUS_SHA $VERCEL_GIT_COMMIT_SHA --quiet -- . && exit 0 || exit 1- Pros: Checks all commits in a push
- Cons: Can be unpredictable for first deployments or branch switches
if [[ "$VERCEL_GIT_COMMIT_REF" != "main" ]]; then exit 1; fi; git diff HEAD^ HEAD --quiet -- . && exit 0 || exit 1- Pros: Best of both worlds - always build features, smart on main
- Cons: Slightly more complex
Running "if [[ "$VERCEL_GIT_COMMIT_REF" != "main" ]]; then exit 1; fi; ..."
✅ - Build can proceed (feature branch)
Building...
Running "if [[ "$VERCEL_GIT_COMMIT_REF" != "main" ]]; then exit 1; fi; ..."
Running git diff HEAD^ HEAD --quiet -- .
⏭️ - The Deployment has been canceled (no changes detected)
Running "if [[ "$VERCEL_GIT_COMMIT_REF" != "main" ]]; then exit 1; fi; ..."
Running git diff HEAD^ HEAD --quiet -- .
✅ - Build can proceed (changes detected)
Building...