Skip to content

feat: support bare repositories with worktrees #4783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sQVe
Copy link

@sQVe sQVe commented Jul 29, 2025

Summary

Enables lazygit to work with bare Git repositories that use worktrees, fixing crashes when running lazygit from directories containing bare repo + worktree setups. This resolves the "fatal: this operation must be run in a work tree" error that prevents lazygit from starting in these advanced Git workflow scenarios.

Problem solved: Previously, running lazygit in a directory like:

/project/
├── .bare/          # bare repository
├── main/           # worktree for main branch
└── .git            # points to .bare

Would crash with: fatal: this operation must be run in a work tree

Solution: lazygit now detects the bare repo setup and automatically opens in the appropriate worktree (e.g., main branch).

Changes

  • Bare repository detection: Added detection logic for common bare repo patterns (.bare, bare.git, .git)
  • Worktree selection: Implemented algorithm to automatically select the best worktree, prioritizing default branch, then main/master, then alphabetical order
  • Comprehensive test coverage: Added 218 lines of tests covering various bare repo + worktree scenarios including edge cases

Test Plan

For reviewers to verify:

  • Test with standard non-bare repositories (existing functionality unchanged)
  • Test bare repository with single worktree (should work automatically)
  • Test bare repository with multiple worktrees (should select main/master branch worktree)
  • Test bare repository with custom default branch (should select correctly)
  • Test error handling for invalid/missing bare repositories
  • Run existing test suite to ensure no regressions: go test ./pkg/commands/git_commands/
  • Test path traversal protection with malicious directory inputs

Enables lazygit to work with bare Git repositories that use worktrees, improving compatibility with advanced Git workflows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant