diff --git a/modules/git/command.go b/modules/git/command.go
index f01db2e1d..33143dbd7 100644
--- a/modules/git/command.go
+++ b/modules/git/command.go
@@ -138,7 +138,7 @@ func (c *Command) RunInDirTimeoutEnvFullPipelineFunc(env []string, timeout time.
 		fn(ctx, cancel)
 	}
 
-	if err := cmd.Wait(); err != nil {
+	if err := cmd.Wait(); err != nil && ctx.Err() != context.DeadlineExceeded {
 		return err
 	}
 
diff --git a/modules/git/command_test.go b/modules/git/command_test.go
index 2f35b3c32..00801ae31 100644
--- a/modules/git/command_test.go
+++ b/modules/git/command_test.go
@@ -30,7 +30,7 @@ func TestRunInDirTimeoutPipelineAlwaysTimeout(t *testing.T) {
 	maxLoops := 1000
 
 	// 'git hash-object --stdin' blocks on stdin so we can have the timeout triggered.
-	cmd := NewCommand("hash-object --stdin")
+	cmd := NewCommand("hash-object", "--stdin")
 	for i := 0; i < maxLoops; i++ {
 		if err := cmd.RunInDirTimeoutPipeline(1*time.Microsecond, "", nil, nil); err != nil {
 			if err != context.DeadlineExceeded {