diff --git a/spec/bundler/support/helpers.rb b/spec/bundler/support/helpers.rb
index 731a2d9664..586603fbd7 100644
--- a/spec/bundler/support/helpers.rb
+++ b/spec/bundler/support/helpers.rb
@@ -329,9 +329,10 @@ module Spec
 
         replace_version_file(version, dir: build_path) # rubocop:disable Style/HashSyntax
 
+        sys_exec("git rev-parse --short HEAD", :dir => source_root)
         build_metadata = {
           :built_at => loaded_gemspec.date.utc.strftime("%Y-%m-%d"),
-          :git_commit_sha => sys_exec("git rev-parse --short HEAD", :dir => source_root).strip,
+          :git_commit_sha => exitstatus.zero? ? out.strip : "unknown",
         }
 
         replace_build_metadata(build_metadata, dir: build_path) # rubocop:disable Style/HashSyntax