Title: `Repo()` fails in Git Worktree when `GIT_DIR` environment variable is set · Issue #2022 · gitpython-developers/GitPython · GitHub
Open Graph Title: `Repo()` fails in Git Worktree when `GIT_DIR` environment variable is set · Issue #2022 · gitpython-developers/GitPython
X Title: `Repo()` fails in Git Worktree when `GIT_DIR` environment variable is set · Issue #2022 · gitpython-developers/GitPython
Description: The git.Repo() constructor in GitPython fails to correctly initialize a repository when called without a path from within a Git worktree and the GIT_DIR environment variable is set to that worktree. This variable is often automatically s...
Open Graph Description: The git.Repo() constructor in GitPython fails to correctly initialize a repository when called without a path from within a Git worktree and the GIT_DIR environment variable is set to that worktree...
X Description: The git.Repo() constructor in GitPython fails to correctly initialize a repository when called without a path from within a Git worktree and the GIT_DIR environment variable is set to that worktree...
Opengraph URL: https://github.com/gitpython-developers/GitPython/issues/2022
X: @github
Domain: redirect.github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`Repo()` fails in Git Worktree when `GIT_DIR` environment variable is set","articleBody":"The `git.Repo()` constructor in GitPython fails to correctly initialize a repository when called without a path from within a Git *worktree* and the `GIT_DIR` environment variable is set to that worktree. This variable is often automatically set by Git when executing an alias to point to the current repo or worktree git_dir. While `Repo(os.getcwd())` finds the worktree correctly, calling `Repo()` with `GIT_DIR` set to `Repo(os.getcwd()).git_dir` raises an `InvalidGitRepositoryError`.\n\n\n**Minimal Reproducing Example:**\n\n1. **Set up a main Git repository:**\n\n ```bash\n mkdir main_repo\n cd main_repo\n git init\n touch initial_file.txt\n git add initial_file.txt\n git commit -m \"Initial commit\"\n ```\n\n2. **Create a Git worktree:**\n\n ```bash\n git worktree add worktree_a\n cd worktree_a\n ```\n\n3. **Create a simple Python script (e.g., `test_repo.py`) to initialize a Repo object:**\n\n```python\nimport os\nfrom git import Repo, InvalidGitRepositoryError\n\nprint(f\"Current working directory: {os.getcwd()}\")\nprint(f\"GIT_DIR environment variable: {os.getenv('GIT_DIR')}\")\n\ntry:\n repo = Repo()\n print(f\"Repo initialized successfully: {repo.working_dir}\")\nexcept InvalidGitRepositoryError as e:\n print(f\"Error initializing Repo(): {e}\")\n\ntry:\n repo_with_cwd = Repo(os.getcwd())\n print(f\"Repo initialized with cwd successfully: {repo_with_cwd.working_dir}\")\n print(f\"Repo(os.getcwd()).git_dir: {Repo(os.getcwd()).git_dir}\")\nexcept InvalidGitRepositoryError as e:\n print(f\"Error initializing Repo(os.getcwd()): {e}\")\n```\n\n4. **Create a Git alias to run the Python script (e.g., in `.git/config` or using `git config --local alias.test-repo '!python /path/to/test_repo.py'`):**\n\n Replace `/path/to/test_repo.py` with the actual path to your script.\n\n5. **Run the Git alias from within the worktree:**\n\n ```bash\n git test-repo\n ```\n\n**Expected Behavior:**\n\n`Repo()` should successfully initialize the repository, recognizing the worktree's context and linking back to the main repository's `.git` directory.\n\n**Observed Behavior:**\n\nRaises an `InvalidGitRepositoryError` when `Repo()` is called without a path, while `Repo(os.getcwd())` succeeds. The output will also show that the `GIT_DIR` environment variable is set within the worktree's context.\n\n**Additional Information:**\n\n* **GitPython Version:** 3.1.44\n* **Python Version:** 3.10.14\n* **Operating System:** Linux\n\nMaybe one should also check if such happens for submodules since they also have the repo dir in a different place than the normal .git dir.\n\n**Workaround:**\n\nExplicitly passing `os.getcwd()` to the `Repo()` constructor or unsetting the `GIT_DIR` environment variable before calling `Repo()` seems to work.\n","author":{"url":"https://github.com/Leokap","@type":"Person","name":"Leokap"},"datePublished":"2025-04-08T14:26:11.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/2022/GitPython/issues/2022"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1d9bd637-d934-c893-d15f-f35f14237f1e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BAE8:2D0DCE:911933:C2BC63:69697C73 |
| html-safe-nonce | 29e9bd8c522bdc31f28d1faf5fbac9c2cc403ae45e3c49152b2f46d54da01ab5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQUU4OjJEMERDRTo5MTE5MzM6QzJCQzYzOjY5Njk3QzczIiwidmlzaXRvcl9pZCI6Ijg0ODQyNTQ0MDIzMDQ2MzgwNjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | aa81354ae9e1e732d2bb8a8de2895a7d04926484c791923c4f69f556d84bae96 |
| hovercard-subject-tag | issue:2979978418 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/gitpython-developers/GitPython/2022/issue_layout |
| twitter:image | https://opengraph.githubassets.com/969626cc55f98c5332389996a279af0d99bab1c1738578ac6801e9009933d064/gitpython-developers/GitPython/issues/2022 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/969626cc55f98c5332389996a279af0d99bab1c1738578ac6801e9009933d064/gitpython-developers/GitPython/issues/2022 |
| og:image:alt | The git.Repo() constructor in GitPython fails to correctly initialize a repository when called without a path from within a Git worktree and the GIT_DIR environment variable is set to that worktree... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Leokap |
| hostname | github.com |
| expected-hostname | github.com |
| None | f33e4b94c8824ab2b434d82a94139432fb5ebee9df4b75304140ad22508c4a77 |
| turbo-cache-control | no-preview |
| go-import | github.com/gitpython-developers/GitPython git https://github.com/gitpython-developers/GitPython.git |
| octolytics-dimension-user_id | 503709 |
| octolytics-dimension-user_login | gitpython-developers |
| octolytics-dimension-repository_id | 1126087 |
| octolytics-dimension-repository_nwo | gitpython-developers/GitPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1126087 |
| octolytics-dimension-repository_network_root_nwo | gitpython-developers/GitPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 48f380098b30acbb700b04f1724481ca10d574fc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width