plandex-ai/plandex · error

error adding files to git repository for dir: %s, err: %v

Error message

error adding files to git repository for dir: %s, err: %v

What it means

GitAddAndCommit stages all changes ('.') in dir via GitAdd and wraps any failure with this message. GitAdd shells out to `git -C <dir> add .`, so this error means the git add subprocess failed — usually because dir is not a git repository or contains no addable state.

Source

Thrown at app/cli/lib/git.go:23

	"log"
	"os/exec"
	"regexp"
	"strings"
	"sync"
	"time"
)

var gitMutex sync.Mutex

func GitAddAndCommit(dir, message string, lockMutex bool) error {
	if lockMutex {
		gitMutex.Lock()
		defer gitMutex.Unlock()
	}

	err := GitAdd(dir, ".", false)
	if err != nil {
		return fmt.Errorf("error adding files to git repository for dir: %s, err: %v", dir, err)
	}

	err = GitCommit(dir, message, nil, false)
	if err != nil {
		return fmt.Errorf("error committing files to git repository for dir: %s, err: %v", dir, err)
	}

	return nil
}

func GitAddAndCommitPaths(dir, message string, paths []string, lockMutex bool) error {
	if len(paths) == 0 {
		return nil
	}

	if lockMutex {
		gitMutex.Lock()
		defer gitMutex.Unlock()

View on GitHub (pinned to e2d772072e)

Solutions

  1. Verify dir is a git repository: run `git -C <dir> status`; if it fails, run `git -C <dir> init`.
  2. Check the wrapped err/output (GitAdd includes CombinedOutput) for the exact git failure (e.g. 'not a git repository', 'fatal: ...').
  3. Ensure git is installed and on PATH in the environment.
  4. Fix .git permissions if the index.lock is unwritable, and remove a stale .git/index.lock if present.

Example fix

// before
# dir lost its .git after an interrupted update
// after
git -C <dir> init && retry the plandex operation
(or restore the plans directory from backup)
Defensive patterns

Strategy: validation

Validate before calling

// Verify the target is a git repo before GitAddAndCommit
import "os/exec"
func isGitRepo(dir string) bool {
    return exec.Command("git", "-C", dir, "rev-parse", "--git-dir").Run() == nil
}
// if !isGitRepo(dir) { git -C dir init || fix dir }

Type guard

func isGitAddError(err error) bool {
    return err != nil && strings.Contains(err.Error(), "error adding files to git repository")
}

Try / catch

if err := lib.GitAddAndCommit(dir, msg, true); err != nil {
    if isGitAddError(err) {
        log.Printf("git add failed for %s: %v", dir, err)
        // inspect .git state, possibly re-init
        return
    }
    return err
}

Prevention

When it happens

Trigger: Calling GitAddAndCommit with a dir that is not a git repository (no .git), a nonexistent dir, a corrupted .git, or with git not on PATH; the wrapped error carries the exec output from GitAdd.

Common situations: Project plans directory missing its .git after interrupted setup or manual cleanup; passing the wrong directory path; CI environments where git is not installed; permissions on .git preventing writes to the index.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/8040aa3c14d8f0e0. Report an issue: GitHub.