From 7c184ed11e07f92432a6eb966ab2ce59d30fbeed Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Mon, 15 Sep 2025 14:24:36 +0200 Subject: [PATCH] git:cow: Fix GIT_DIR for submodules via git alias Git sets some environment variables when executing a shell command. Specifically it sets GIT_DIR when called inside a submodule. This makes git inside the subshell target the main worktree instead of the temporary one. --- .config/zsh/autoload/git/git-checkout-worktree | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.config/zsh/autoload/git/git-checkout-worktree b/.config/zsh/autoload/git/git-checkout-worktree index 00bc271..817d33f 100755 --- a/.config/zsh/autoload/git/git-checkout-worktree +++ b/.config/zsh/autoload/git/git-checkout-worktree @@ -60,6 +60,17 @@ until [[ -d $cwd_offset || -z $cwd_offset ]]; do done [[ -z $cwd_offset ]] || cd "$cwd_offset" +# Discard some environment variables that were set by git when calling this +# script through an alias. +# +# Is set for submodules and will confuse git in the subshell +unset GIT_DIR +# Not sure if this can bring any issues, but better be safe +unset GIT_PREFIX +# TODO: Do we want to unset this too? Could have been set on purpose by the user +# and not git. Maybe only for the subshell via `env`? +#unset GIT_EXEC_PATH + "$SHELL" && errc=$? || errc=$? (( !errc )) || echo "shell exited with $errc"