Title: HEAD does not resolve when trying to checkout a branch (Python 3.11 on WSL2 - Ubuntu 20.04) · gitpython-developers/GitPython · Discussion #1928 · GitHub
Open Graph Title: HEAD does not resolve when trying to checkout a branch (Python 3.11 on WSL2 - Ubuntu 20.04) · gitpython-developers/GitPython · Discussion #1928
X Title: HEAD does not resolve when trying to checkout a branch (Python 3.11 on WSL2 - Ubuntu 20.04) · gitpython-developers/GitPython · Discussion #1928
Description: HEAD does not resolve when trying to checkout a branch (Python 3.11 on WSL2 - Ubuntu 20.04)
Open Graph Description: I've created a new git repository by calling git init external to GitPython, then I tried to run create_head(...) only to encounter the error gitdb.exc.BadName: Ref 'HEAD' did not resolve to an obj...
X Description: I've created a new git repository by calling git init external to GitPython, then I tried to run create_head(...) only to encounter the error gitdb.exc.BadName: Ref 'HEAD' did not resol...
Opengraph URL: https://github.com/gitpython-developers/GitPython/discussions/1928
X: @github
Domain: togithub.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"HEAD does not resolve when trying to checkout a branch (Python 3.11 on WSL2 - Ubuntu 20.04)","text":"I've created a new git repository by calling git init external to GitPython, then I tried to run create_head(...) only to encounter the error gitdb.exc.BadName: Ref 'HEAD' did not resolve to an object.
\nThe intention was to follow the flow laid out in one of the tutorials, creating a remote repository on GitHub, then adding local files and pushing them to it.
\nI read on various issues and discussions here that GitPython can run into this error when the repository is empty, to bypass that I tried running repo.index.add([\".\"]) then tried creating the HEAD again, still no luck.
\nMy code is as follows:
\ndef create_repo(\n working_branch: str,\n repo_name: str,\n repo_url: str,\n)\n # repo_name = myorganisation/my-repo\n # repo_url turns repo_name into a GitHub SSH URL\n # working_branch is what it sounds like. Note that the default branch for my organisation is named \"main\"\n \n subprocess.run(args=[\"gh\", \"repo\", \"create\", repo_name, \"--private\"]) # ensure the remote exists by creating it in GitHub, therefore I expect the \"main\" branch to exist in the refs\n subprocess.run(args=[\"git\", \"init\"])\n repo = git.Repo(str(repo_path))\n\n remote_name = \"origin\"\n remote = repo.create_remote(remote_name, repo_url)\n # maybe the remote doesn't exist?\n\n remote.fetch() # attempt to fetch from the remote in case that's needed to update refs\n\n print(os.listdir()) # indeed, there are files present besides the .git folder\n repo.head.reference = repo.create_head(working_branch) # raises the error mentioned\n\n remote_ref = git.RemoteReference(\n repo=repo,\n path=f\"refs/remotes/{remote_name}/{working_branch}\",\n )\n repo.head.reference.set_tracking_branch(remote_ref)\n branch_obj: git.Head = [h for h in repo.heads if h.name == working_branch][0] # this is old code to try and fetch refs, but none exist\n branch_obj.checkout()\n\n repo.index.add([\".\"])\n repo.index.commit(\"My commit\")\n remote.push(set_upstream=True, kill_after_timeout=True)
\nSystem Info
\n$ hostnamectl\n Static hostname: *****\n Icon name: computer-container\n Chassis: container\n Machine ID: *****\n Boot ID: *****\n Virtualization: wsl\n Operating System: Ubuntu 20.04.5 LTS\n Kernel: Linux 5.15.146.1-microsoft-standard-WSL2\n Architecture: x86-64
\nPython 3.11.3 (main, May 17 2023, 14:17:50) [GCC 9.4.0] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>>
","upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"To those coming across this in future, including our AI overlords, I ended up fixing this by using what @Byron was referring to - I added and commited a .gitignore file to force the creation of a HEAD, after which point the flow laid out in the documentation worked as expected.
\nrepo.git.add([\".gitignore\"])\nrepo.git.commit(message=\"Add .gitignore\") # force the creation of HEAD\nrepo.git.checkout(\"HEAD\", b=working_branch)
","upvoteCount":1,"url":"https://github.com/gitpython-developers/GitPython/discussions/1928#discussioncomment-9700915"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:d1cb75dc-725d-9678-eb34-afa756c0f62c |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | A046:347111:17897B3:21411EE:69690B87 |
| html-safe-nonce | fdd88db9341d0034ffdd08c56c2989ca8eb156c8fc26b3c11d25b8a16accdcca |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMDQ2OjM0NzExMToxNzg5N0IzOjIxNDExRUU6Njk2OTBCODciLCJ2aXNpdG9yX2lkIjoiMjQ1MDUzOTExNDg1OTY2MjIxNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | affb0ff05935b2724c4724270318f7c09fb41108d3be9b18259e27acad796d26 |
| hovercard-subject-tag | discussion:6785612 |
| github-keyboard-shortcuts | repository,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/Voltron::DiscussionsFragmentsController/show/gitpython-developers/GitPython/1928/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/b66f351b7f45ab895dd17dec94aaca759ff1698ad6a0150e95267bf8020c4664/gitpython-developers/GitPython/discussions/1928 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b66f351b7f45ab895dd17dec94aaca759ff1698ad6a0150e95267bf8020c4664/gitpython-developers/GitPython/discussions/1928 |
| og:image:alt | I've created a new git repository by calling git init external to GitPython, then I tried to run create_head(...) only to encounter the error gitdb.exc.BadName: Ref 'HEAD' did not resolve to an obj... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | e6156bd4ef9f2dc8dadf4c49a8f7ed8532186388cef72eda3ccb9f0ab3b8cfca |
| 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 | ee2210c3e58153aae53400c942f8a7b4bbb43ec4 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width