René's URL Explorer Experiment


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

direct link

Domain: redirect.github.com


Hey, it has json ld scripts:
{"@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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:1d9bd637-d934-c893-d15f-f35f14237f1e
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBAE8:2D0DCE:911933:C2BC63:69697C73
html-safe-nonce29e9bd8c522bdc31f28d1faf5fbac9c2cc403ae45e3c49152b2f46d54da01ab5
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQUU4OjJEMERDRTo5MTE5MzM6QzJCQzYzOjY5Njk3QzczIiwidmlzaXRvcl9pZCI6Ijg0ODQyNTQ0MDIzMDQ2MzgwNjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacaa81354ae9e1e732d2bb8a8de2895a7d04926484c791923c4f69f556d84bae96
hovercard-subject-tagissue:2979978418
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/gitpython-developers/GitPython/2022/issue_layout
twitter:imagehttps://opengraph.githubassets.com/969626cc55f98c5332389996a279af0d99bab1c1738578ac6801e9009933d064/gitpython-developers/GitPython/issues/2022
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/969626cc55f98c5332389996a279af0d99bab1c1738578ac6801e9009933d064/gitpython-developers/GitPython/issues/2022
og:image:altThe 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameLeokap
hostnamegithub.com
expected-hostnamegithub.com
Nonef33e4b94c8824ab2b434d82a94139432fb5ebee9df4b75304140ad22508c4a77
turbo-cache-controlno-preview
go-importgithub.com/gitpython-developers/GitPython git https://github.com/gitpython-developers/GitPython.git
octolytics-dimension-user_id503709
octolytics-dimension-user_logingitpython-developers
octolytics-dimension-repository_id1126087
octolytics-dimension-repository_nwogitpython-developers/GitPython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id1126087
octolytics-dimension-repository_network_root_nwogitpython-developers/GitPython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release48f380098b30acbb700b04f1724481ca10d574fc
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://redirect.github.com/gitpython-developers/GitPython/issues/2022#start-of-content
https://redirect.github.com/
Sign in https://redirect.github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgitpython-developers%2FGitPython%2Fissues%2F2022
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://redirect.github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgitpython-developers%2FGitPython%2Fissues%2F2022
Sign up https://redirect.github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=gitpython-developers%2FGitPython
Reloadhttps://redirect.github.com/gitpython-developers/GitPython/issues/2022
Reloadhttps://redirect.github.com/gitpython-developers/GitPython/issues/2022
Reloadhttps://redirect.github.com/gitpython-developers/GitPython/issues/2022
gitpython-developers https://redirect.github.com/gitpython-developers
GitPythonhttps://redirect.github.com/gitpython-developers/GitPython
Please reload this pagehttps://redirect.github.com/gitpython-developers/GitPython/issues/2022
Notifications https://redirect.github.com/login?return_to=%2Fgitpython-developers%2FGitPython
Fork 964 https://redirect.github.com/login?return_to=%2Fgitpython-developers%2FGitPython
Star 5k https://redirect.github.com/login?return_to=%2Fgitpython-developers%2FGitPython
Code https://redirect.github.com/gitpython-developers/GitPython
Issues 169 https://redirect.github.com/gitpython-developers/GitPython/issues
Pull requests 8 https://redirect.github.com/gitpython-developers/GitPython/pulls
Discussions https://redirect.github.com/gitpython-developers/GitPython/discussions
Actions https://redirect.github.com/gitpython-developers/GitPython/actions
Security Uh oh! There was an error while loading. Please reload this page. https://redirect.github.com/gitpython-developers/GitPython/security
Please reload this pagehttps://redirect.github.com/gitpython-developers/GitPython/issues/2022
Insights https://redirect.github.com/gitpython-developers/GitPython/pulse
Code https://redirect.github.com/gitpython-developers/GitPython
Issues https://redirect.github.com/gitpython-developers/GitPython/issues
Pull requests https://redirect.github.com/gitpython-developers/GitPython/pulls
Discussions https://redirect.github.com/gitpython-developers/GitPython/discussions
Actions https://redirect.github.com/gitpython-developers/GitPython/actions
Security https://redirect.github.com/gitpython-developers/GitPython/security
Insights https://redirect.github.com/gitpython-developers/GitPython/pulse
New issuehttps://redirect.github.com/login?return_to=https://github.com/gitpython-developers/GitPython/issues/2022
New issuehttps://redirect.github.com/login?return_to=https://github.com/gitpython-developers/GitPython/issues/2022
Repo() fails in Git Worktree when GIT_DIR environment variable is sethttps://redirect.github.com/gitpython-developers/GitPython/issues/2022#top
acknowledgedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22acknowledged%22
help wantedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22help%20wanted%22
https://github.com/Leokap
https://github.com/Leokap
Leokaphttps://github.com/Leokap
on Apr 8, 2025https://github.com/gitpython-developers/GitPython/issues/2022#issue-2979978418
acknowledgedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22acknowledged%22
help wantedhttps://github.com/gitpython-developers/GitPython/issues?q=state%3Aopen%20label%3A%22help%20wanted%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.