git🐮 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.
This commit is contained in:
2025-09-15 14:24:36 +02:00
parent 8e8ef29b37
commit 7c184ed11e

View File

@@ -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"