-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
base: master
Are you sure you want to change the base?
Conversation
Enables lazygit to work with bare Git repositories that use worktrees, improving compatibility with advanced Git workflows.
Sorry for the delay in looking at this. I'm not experienced with either bare repos or worktrees (even in non-bare repos), so maybe these are stupid questions, but I don't understand why you can't simply cd into your worktree and run lazygit there. In my brief testing, this worked fine with a worktree that is attached to a bare repo. But I also don't understand what |
No problem at all. I've been real busy also 😉
Yes, of course! There is a worktree structure that is considered best practice, and most common. This pattern is recommended because it:
You structure your project like this: project/
├── .bare/ # The bare repository
├── feat-example/ # Worktree for feat-example branch
├── hotfix/ # Worktree for quick hotfixes
├── main/ # Worktree for main branch
└── .git # Optional: Git file with `gitdir: .bare` that makes regular git commands work from project root This allows you to get a great overview of your worktrees, while keeping your version history at one place. You are essentially creating a pool of worktrees that you can move in and out of. Current behavior (crashes): cd /project
lazygit
# Error: fatal: this operation must be run in a work tree Expected behavior (with this PR): cd /project
lazygit
# Automatically detects bare repo setup and opens in main worktree Why not just "
|
Thanks for the explanation, makes a lot of sense. The setup sounds very useful, and I'm probably going to switch to that setup for my work repos. However, I'm not convinced yet that being able to open lazygit from I feel that we shouldn't weaken the requirement that lazygit needs a worktree in order to work. Only work trees show up in the recent repos list; the I'm still new to this workflow, so maybe I'm missing things here. Will play with this some more to get a better feeling for it. |
I switched my repos at work to this setup, and have been liking it a lot. (I omitted the Working with this setup didn't change my mind from what I wrote above: I simply don't expect to be able to start lazygit in the However, there are a few bugs around starting lazygit in a bare repo:
Once these are fixed, I could also imagine an improvement to how we pick the most recent repo to open: if you open lazygit in a bare repo, it could check for worktrees attached to that bare repo, and see if your recent repos list contains any of those. If so, pick the first (most recent) one, and otherwise open the first recent repo as normal. I suppose this would mostly give you the functionality that you were after with this PR, except for the very first time you open lazygit in |
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: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
,bare.git
,.git
)Test Plan
For reviewers to verify:
go test ./pkg/commands/git_commands/