fix(cli): preserve .git directory when scaffolding into a non-empty folder#3740
Open
itsjustriley wants to merge 2 commits intomainfrom
Open
fix(cli): preserve .git directory when scaffolding into a non-empty folder#3740itsjustriley wants to merge 2 commits intomainfrom
itsjustriley wants to merge 2 commits intomainfrom
Conversation
When scaffolding Hydrogen into a non-empty directory, the CLI now preserves any existing .git directory or file (gitfile for submodule worktrees) instead of deleting it along with other contents. This allows users to safely add a Hydrogen storefront to an existing repository without losing version history. The confirmation prompt has been updated to explicitly state that .git is preserved while other contents are deleted. Fixes dt#1201 Closes #2989
a1c4447 to
15e9e68
Compare
Contributor
|
Oxygen deployed a preview of your
Learn more about Hydrogen's GitHub integration. |
andguy95
reviewed
Apr 21, 2026
Address review feedback from @andguy95: 1. **Conditional .git messaging**: The confirmation prompt only mentions ".git is kept" when .git actually exists in the directory. Previously, the message appeared even when scaffolding into directories without git. 2. **Abort handler gap**: The abort handler now uses clearDirectoryPreservingGit() instead of rmdir(force: true), ensuring .git is preserved even when scaffolding fails mid-process (network errors, Ctrl+C, etc.). 3. **.git-only directories**: Skip the confirmation prompt entirely when the directory contains only .git (nothing to delete). This avoids a misleading "delete contents" prompt when there are no contents to delete. New helper functions: - hasGitDirectory(): Check if .git exists (file or directory) - hasOnlyGitDirectory(): Check if directory contains only .git Tests added for all three behaviors. Co-Authored-By: Claude Opus 4.5 (1M context) <noreply@anthropic.com>
andguy95
approved these changes
Apr 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WHY are these changes introduced?
Fixes dt#1201 · Closes #2989
Running
npm create @shopify/hydrogen@latestin a directory that already contains a git repo wipes out the.git/folder, taking the entire commit history with it. People hit this when they want to start with a blank repo + README and scaffold Hydrogen on top — a pretty common way to start a project.The usual workaround is to
mv .git ../tmp-git, scaffold, then move it back. That's fragile, not obvious, and nobody should have to think about it.WHAT is this pull request doing?
When the CLI clears a non-empty target directory during scaffolding, it now skips
.git/(both directory and gitfile forms). Everything else is deleted as before.Two small copy updates so users know what's happening:
Xis not empty. Continuing will delete its contents (your.gitdirectory is kept). Continue?"--forceor-fto delete its contents (.gitis always preserved)."A few deliberate scope choices:
.git/is the right default; an opt-in--preserve-gitflag would just be surface area nobody sets correctly..git/is carved out..gitignore,.env, and.shopify/are legitimate scaffold-owned files, so they're deleted as before.@shopify/cli-hydrogenand@shopify/create-hydrogenbumped. Per CLAUDE.md, changes to theinitcommand path require both packages to be bumped so new scaffolds pick up the fix.HOW to test your changes?
Everything below runs in a throwaway
$TMPDIRdirectory. Your real.gitfolders outside the sandbox are never touched.Setup (once):
Scenario 1 — the bug (non-empty dir with existing
.git):What to check:
.gitdirectory is kept"..git/still exists after the scaffold.git log --onelineshows both your"Pre-existing project"commit and the new"Scaffold Storefront"commit on top.src/app.jsis gone (not part of the skeleton);README.mdis now the skeleton's README.Scenario 2 — decline the prompt:
What to check:
.gitis always preserved".keepme.txtand.git/both survive.Scenario 3 —
--forcepath (no prompt, still preserves.git):What to check:
.git/exists.Cleanup:
Empty directories and non-empty directories without
.git/are covered by automated tests inpackages/cli/src/lib/onboarding/common.test.ts, so no need to tophat those manually.Checklist