Skip to content

Instantly share code, notes, and snippets.

@statik
Created July 18, 2025 15:25
Show Gist options
  • Save statik/5854ca7a8c599f1531a5db4a3626ef19 to your computer and use it in GitHub Desktop.
Save statik/5854ca7a8c599f1531a5db4a3626ef19 to your computer and use it in GitHub Desktop.
git post-checkout hook to copy .envrc files from main worktree
#!/bin/bash
# if we are checking out a new git worktree, then copy over the
# environment files
if [[ "$1" == "0000000000000000000000000000000000000000" ]]; then
basePath=$(git rev-parse --path-format=absolute --git-common-dir | xargs dirname)
# Use glob to find all files starting with .env
echo "running git post-checkout hook to copy .env* files to new worktree"
for envFile in "$basePath"/.env*; do
if [[ -f "$envFile" ]]; then
filename=$(basename "$envFile")
# Check if the file is not tracked by git
if ! git -C "$basePath" ls-files --error-unmatch "$filename" >/dev/null 2>&1; then
echo "Copying untracked file: $envFile to $(pwd)/$filename"
cp "$envFile" "$(pwd)/$filename"
fi
fi
done
fi
@statik
Copy link
Author

statik commented Jul 18, 2025

drop this in your .git/hooks folder without the .sh extension and chmod +x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment