diff --git a/config/software/git.rb b/config/software/git.rb
index fe209337dcda9d213e5be4b1d16726d649f02041_Y29uZmlnL3NvZnR3YXJlL2dpdC5yYg==..8b797ffea9d8697199ccecbcb4e7c0ba55b285ba_Y29uZmlnL3NvZnR3YXJlL2dpdC5yYg== 100644
--- a/config/software/git.rb
+++ b/config/software/git.rb
@@ -76,7 +76,16 @@
   sm_version_override_git_repo_url = Gitlab::Util.get_env('SELF_MANAGED_VERSION_REGEX_OVERRIDE_GIT_REPO_URL')
   git_repo_url = Gitlab::Util.get_env('GITALY_GIT_REPO_URL')
   if Build::Check.is_auto_deploy_tag?
-    git_version_2_37_1 = Gitlab::Util.get_env('GITALY_GIT_VERSION_2_37_1')
-    git_version_2_38 = Gitlab::Util.get_env('GITALY_GIT_VERSION_2_38')
+    # Gitaly potentially bundles multiple different Git distributions with it.
+    # It is possible to override the specific version that Gitaly compiles each
+    # of these distributions with by setting:
+    #
+    #     `GIT_VERSION_2_38=v2.38.1`
+    #
+    # As the bundled Git versions change over time we have this generic loop to
+    # just accept any such override into the environment used by make.
+    ENV.select { |k, v| k.start_with('GITALY_GIT_VERSION_') }.each do |k, v|
+      env[k.delete_prefix('GITALY_')] = v unless v&.empty?
+    end
 
     env['GIT_REPO_URL'] = git_repo_url if git_repo_url
@@ -81,8 +90,5 @@
 
     env['GIT_REPO_URL'] = git_repo_url if git_repo_url
-    env['GIT_VERSION_2_37_1'] = git_version_2_37_1 if git_version_2_37_1
-    env['GIT_VERSION_2_38'] = git_version_2_38 if git_version_2_38
-
   elsif sm_version_override_git_repo_url && Regexp.new(sm_version_override_git_repo_url).match?(Build::Info.gitlab_version)
     env['GIT_REPO_URL'] = git_repo_url if git_repo_url
   end