plandex-ai/plandex · error
error getting current git branch for dir: %s, err: %v
Error message
error getting current git branch for dir: %s, err: %v
What it means
gitCheckoutBranch first queries the currently checked-out branch with `git -C repoDir branch --show-current` so it can no-op when the target branch is already active (checking out the same branch would error). If that query command itself fails to run or exits non-zero, this error wraps the directory and underlying error. Callers include lockRepoDB, so this can block repo locking flows.
Source
Thrown at app/server/db/git.go:441
}
return nil
}
func gitCheckoutBranch(repoDir, branch string) error {
log.Printf("[Git] gitCheckoutBranch - repoDir: %s, branch: %s", repoDir, branch)
if err := gitRemoveIndexLockFileIfExists(repoDir); err != nil {
return fmt.Errorf("error removing lock file before checkout: %v", err)
}
// get current branch and only checkout if it's not the same
// trying to check out the same branch will result in an error
var out bytes.Buffer
cmd := exec.Command("git", "-C", repoDir, "branch", "--show-current")
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return fmt.Errorf("error getting current git branch for dir: %s, err: %v", repoDir, err)
}
currentBranch := strings.TrimSpace(out.String())
log.Printf("[Git] gitCheckoutBranch - currentBranch: %s", currentBranch)
if currentBranch == branch {
log.Printf("[Git] gitCheckoutBranch - already on branch %s, skipping", branch)
return nil
}
log.Println("[Git] gitCheckoutBranch - checking out branch:", branch)
res, err := exec.Command("git", "-C", repoDir, "checkout", branch).CombinedOutput()
if err != nil {
return fmt.Errorf("error checking out git branch for dir: %s, err: %v, output: %s", repoDir, err, string(res))
}
return nil
}
View on GitHub (pinned to e2d772072e)
Solutions
- Run `git -C <repoDir> branch --show-current` manually as the server user to see the raw error.
- Verify repoDir exists and contains .git; recreate or repair the repo if missing.
- Install/repair git and ensure PATH for the service process includes it.
- Note: on a detached HEAD this command exits 0 with empty output — if you also see [367]-style empty results, handle the empty currentBranch case rather than assuming a failure.
Example fix
// before
cmd := exec.Command("git", "-C", repoDir, "branch", "--show-current")
cmd.Stdout = &out
err := cmd.Run()
// after (fail fast, clearer diagnostics)
if _, err := exec.LookPath("git"); err != nil {
return fmt.Errorf("git not found in PATH: %w", err)
}
if _, err := os.Stat(filepath.Join(repoDir, ".git")); err != nil {
return fmt.Errorf("not a git repo (%s): %w", repoDir, err)
} Defensive patterns
Strategy: validation
Validate before calling
func ensureCheckoutable(repoDir string) error {
if _, err := exec.LookPath("git"); err != nil {
return fmt.Errorf("git not in PATH")
}
fi, err := os.Stat(filepath.Join(repoDir, ".git"))
if err != nil {
return fmt.Errorf("%s is not a git repo: %w", repoDir, err)
}
_ = fi
return nil
} Try / catch
if err := gitCheckoutBranch(repoDir, branch); err != nil {
if strings.Contains(err.Error(), "error getting current git branch") {
// environment/repo problem, not a branch problem: check git + repo dir, then retry once
log.Printf("cannot query current branch in %s: %v", repoDir, err)
}
} Prevention
- Install git in all deployment images and verify PATH for the service user.
- Assert repo directories exist and contain .git before any git operation.
- Watch for detached-HEAD states: `branch --show-current` returns empty there; handle empty output explicitly.
- Keep repo dirs on stable mounts with consistent ownership.
When it happens
Trigger: exec of `git branch --show-current` fails: git not installed/not on PATH, repoDir does not exist or is not a git repo, permission denied, or the process environment cannot exec git (missing libs, sandbox).
Common situations: Detached HEAD state (show-current prints empty — not an error, but check surrounding logic); repo directory deleted or moved; git binary missing in container images; path permission issues after volume remounts.
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
- error getting git root: %s
- error getting git status: %s
- error getting files in git repo: %s
- error getting untracked files in git repo: %s
- error adding files to git repository for dir: %s, err: %v
AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05).
Data as JSON: /api/errors/c58ac722ce7eaa6d.
Report an issue: GitHub.