Bug: OpenCode auto-stages untracked files when GIT_INDEX_FILE is set (pre-commit hook context) #6763

Open
opened 2026-02-16 18:05:12 -05:00 by yindo · 1 comment
Owner

Originally created by @Weizhena on GitHub (Jan 19, 2026).

Originally assigned to: @rekram1-node on GitHub.

Description

When running OpenCode in a git pre-commit hook context (where GIT_INDEX_FILE environment variable is set), OpenCode automatically stages untracked files as a side effect, even though no git add command is executed.

Environment

  • OpenCode version: Latest (installed via bun)
  • OS: Linux (WSL2)
  • Shell: bash

Steps to Reproduce

# 1. Setup test files
echo "staged content" > staged.txt
echo "unstaged content" > unstaged.txt
git add staged.txt

# 2. Verify initial state
git status --short
# Expected output:
# A  staged.txt
# ?? unstaged.txt

# 3. Run OpenCode WITHOUT GIT_INDEX_FILE (works correctly)
opencode run "@code-reviewer Review staged changes. Use git diff --cached."
git status --short
# Output: A  staged.txt, ?? unstaged.txt (correct - no change)

# 4. Run OpenCode WITH GIT_INDEX_FILE set (bug occurs)
GIT_INDEX_FILE=.git/index opencode run "@code-reviewer Review staged changes. Use git diff --cached."
git status --short
# Output: A  staged.txt, A  unstaged.txt (BUG - unstaged.txt got staged!)

Expected Behavior

OpenCode should not modify the git staging area. Running OpenCode should be a read-only operation with respect to git state.

Actual Behavior

When GIT_INDEX_FILE environment variable is set (which git automatically sets during pre-commit hooks), OpenCode stages untracked files during its initialization/startup phase, before any agent code runs.

Investigation Details

  • The @code-reviewer agent only ran git diff --cached - it did NOT run git add
  • Debug logging confirmed the staging happens during OpenCode's initialization, not during agent execution
  • The issue does NOT occur when running OpenCode directly (without GIT_INDEX_FILE set)

Workaround

Unset GIT_INDEX_FILE before calling OpenCode in git hooks:

env -u GIT_INDEX_FILE opencode run "..."

Impact

This bug affects anyone using OpenCode in git pre-commit or pre-push hooks for code review, causing unintended files to be staged and potentially committed.

Originally created by @Weizhena on GitHub (Jan 19, 2026). Originally assigned to: @rekram1-node on GitHub. ## Description When running OpenCode in a git pre-commit hook context (where `GIT_INDEX_FILE` environment variable is set), OpenCode automatically stages untracked files as a side effect, even though no `git add` command is executed. ## Environment - OpenCode version: Latest (installed via bun) - OS: Linux (WSL2) - Shell: bash ## Steps to Reproduce ```bash # 1. Setup test files echo "staged content" > staged.txt echo "unstaged content" > unstaged.txt git add staged.txt # 2. Verify initial state git status --short # Expected output: # A staged.txt # ?? unstaged.txt # 3. Run OpenCode WITHOUT GIT_INDEX_FILE (works correctly) opencode run "@code-reviewer Review staged changes. Use git diff --cached." git status --short # Output: A staged.txt, ?? unstaged.txt (correct - no change) # 4. Run OpenCode WITH GIT_INDEX_FILE set (bug occurs) GIT_INDEX_FILE=.git/index opencode run "@code-reviewer Review staged changes. Use git diff --cached." git status --short # Output: A staged.txt, A unstaged.txt (BUG - unstaged.txt got staged!) ``` ## Expected Behavior OpenCode should not modify the git staging area. Running OpenCode should be a read-only operation with respect to git state. ## Actual Behavior When `GIT_INDEX_FILE` environment variable is set (which git automatically sets during pre-commit hooks), OpenCode stages untracked files during its initialization/startup phase, before any agent code runs. ## Investigation Details - The `@code-reviewer` agent only ran `git diff --cached` - it did NOT run `git add` - Debug logging confirmed the staging happens during OpenCode's initialization, not during agent execution - The issue does NOT occur when running OpenCode directly (without `GIT_INDEX_FILE` set) ## Workaround Unset `GIT_INDEX_FILE` before calling OpenCode in git hooks: ```bash env -u GIT_INDEX_FILE opencode run "..." ``` ## Impact This bug affects anyone using OpenCode in git pre-commit or pre-push hooks for code review, causing unintended files to be staged and potentially committed.
yindo added the windows label 2026-02-16 18:05:12 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 19, 2026):

This issue appears to be a duplicate of or closely related to existing issues. Please check:

  • #6553: opencode modifies git index when executed in prepare-commit-msg hook - This describes the exact same issue where OpenCode modifies the git index when run in a git hook context
  • #8694: OpenCode doesnt respect git env vars - This relates to OpenCode not properly respecting git-related environment variables

The root cause is likely the same: OpenCode interacting with git in a way that assumes a standard git context, but behaves unexpectedly when GIT_INDEX_FILE or other git environment variables are set by git hooks.

Feel free to ignore if this is a distinct variation of the issue that hasn't been addressed in those threads.

@github-actions[bot] commented on GitHub (Jan 19, 2026): This issue appears to be a duplicate of or closely related to existing issues. Please check: - #6553: opencode modifies git index when executed in prepare-commit-msg hook - This describes the exact same issue where OpenCode modifies the git index when run in a git hook context - #8694: OpenCode doesnt respect git env vars - This relates to OpenCode not properly respecting git-related environment variables The root cause is likely the same: OpenCode interacting with git in a way that assumes a standard git context, but behaves unexpectedly when GIT_INDEX_FILE or other git environment variables are set by git hooks. Feel free to ignore if this is a distinct variation of the issue that hasn't been addressed in those threads.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#6763