Created
July 18, 2025 15:25
-
-
Save statik/5854ca7a8c599f1531a5db4a3626ef19 to your computer and use it in GitHub Desktop.
git post-checkout hook to copy .envrc files from main worktree
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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
drop this in your
.git/hooks
folder without the.sh
extension andchmod +x