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: Github.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":"\u003cp dir=\"auto\"\u003eI've created a new git repository by calling \u003ccode class=\"notranslate\"\u003egit init\u003c/code\u003e external to GitPython, then I tried to run \u003ccode class=\"notranslate\"\u003ecreate_head(...)\u003c/code\u003e only to encounter the error \u003ccode class=\"notranslate\"\u003egitdb.exc.BadName: Ref 'HEAD' did not resolve to an object\u003c/code\u003e.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThe intention was to follow the flow laid out in \u003ca href=\"https://gitpython.readthedocs.io/en/stable/tutorial.html#handling-remotes\" rel=\"nofollow\"\u003eone of the tutorials\u003c/a\u003e, creating a remote repository on GitHub, then adding local files and pushing them to it.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eI 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 \u003ccode class=\"notranslate\"\u003erepo.index.add([\".\"])\u003c/code\u003e then tried creating the HEAD again, still no luck.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eMy code is as follows:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"def 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 \u0026quot;main\u0026quot;\n \n subprocess.run(args=[\u0026quot;gh\u0026quot;, \u0026quot;repo\u0026quot;, \u0026quot;create\u0026quot;, repo_name, \u0026quot;--private\u0026quot;]) # ensure the remote exists by creating it in GitHub, therefore I expect the \u0026quot;main\u0026quot; branch to exist in the refs\n subprocess.run(args=[\u0026quot;git\u0026quot;, \u0026quot;init\u0026quot;])\n repo = git.Repo(str(repo_path))\n\n remote_name = \u0026quot;origin\u0026quot;\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\u0026quot;refs/remotes/{remote_name}/{working_branch}\u0026quot;,\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([\u0026quot;.\u0026quot;])\n repo.index.commit(\u0026quot;My commit\u0026quot;)\n remote.push(set_upstream=True, kill_after_timeout=True)\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-k\"\u003edef\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003ecreate_repo\u003c/span\u003e(\n \u003cspan class=\"pl-s1\"\u003eworking_branch\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estr\u003c/span\u003e,\n \u003cspan class=\"pl-s1\"\u003erepo_name\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estr\u003c/span\u003e,\n \u003cspan class=\"pl-s1\"\u003erepo_url\u003c/span\u003e: \u003cspan class=\"pl-smi\"\u003estr\u003c/span\u003e,\n)\n \u003cspan class=\"pl-c\"\u003e# repo_name = myorganisation/my-repo\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# repo_url turns repo_name into a GitHub SSH URL\u003c/span\u003e\n \u003cspan class=\"pl-c\"\u003e# working_branch is what it sounds like. Note that the default branch for my organisation is named \"main\"\u003c/span\u003e\n \n \u003cspan class=\"pl-s1\"\u003esubprocess\u003c/span\u003e.\u003cspan class=\"pl-s1\"\u003erun\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eargs\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e[\u003cspan class=\"pl-s\"\u003e\"gh\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"repo\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"create\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003erepo_name\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"--private\"\u003c/span\u003e]) \u003cspan class=\"pl-c\"\u003e# ensure the remote exists by creating it in GitHub, therefore I expect the \"main\" branch to exist in the refs\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003esubprocess\u003c/span\u003e.\u003cspan class=\"pl-s1\"\u003erun\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eargs\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e[\u003cspan class=\"pl-s\"\u003e\"git\"\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\"init\"\u003c/span\u003e])\n \u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003egit\u003c/span\u003e.\u003cspan class=\"pl-v\"\u003eRepo\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003estr\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003erepo_path\u003c/span\u003e))\n\n \u003cspan class=\"pl-s1\"\u003eremote_name\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s\"\u003e\"origin\"\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003eremote\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ecreate_remote\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eremote_name\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003erepo_url\u003c/span\u003e)\n \u003cspan class=\"pl-c\"\u003e# maybe the remote doesn't exist?\u003c/span\u003e\n\n \u003cspan class=\"pl-s1\"\u003eremote\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003efetch\u003c/span\u003e() \u003cspan class=\"pl-c\"\u003e# attempt to fetch from the remote in case that's needed to update refs\u003c/span\u003e\n\n \u003cspan class=\"pl-en\"\u003eprint\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eos\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003elistdir\u003c/span\u003e()) \u003cspan class=\"pl-c\"\u003e# indeed, there are files present besides the .git folder\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ehead\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ereference\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ecreate_head\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eworking_branch\u003c/span\u003e) \u003cspan class=\"pl-c\"\u003e# raises the error mentioned\u003c/span\u003e\n\n \u003cspan class=\"pl-s1\"\u003eremote_ref\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003egit\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eRemoteReference\u003c/span\u003e(\n \u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e,\n \u003cspan class=\"pl-s1\"\u003epath\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-s\"\u003ef\"refs/remotes/\u003cspan class=\"pl-s1\"\u003e\u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eremote_name\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\u003c/span\u003e/\u003cspan class=\"pl-s1\"\u003e\u003cspan class=\"pl-kos\"\u003e{\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eworking_branch\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e}\u003c/span\u003e\u003c/span\u003e\"\u003c/span\u003e,\n )\n \u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ehead\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ereference\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eset_tracking_branch\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eremote_ref\u003c/span\u003e)\n \u003cspan class=\"pl-s1\"\u003ebranch_obj\u003c/span\u003e: \u003cspan class=\"pl-s1\"\u003egit\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eHead\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e [\u003cspan class=\"pl-s1\"\u003eh\u003c/span\u003e \u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eh\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003ein\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eheads\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eif\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eh\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ename\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003e==\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003eworking_branch\u003c/span\u003e][\u003cspan class=\"pl-c1\"\u003e0\u003c/span\u003e] \u003cspan class=\"pl-c\"\u003e# this is old code to try and fetch refs, but none exist\u003c/span\u003e\n \u003cspan class=\"pl-s1\"\u003ebranch_obj\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003echeckout\u003c/span\u003e()\n\n \u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eindex\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eadd\u003c/span\u003e([\u003cspan class=\"pl-s\"\u003e\".\"\u003c/span\u003e])\n \u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eindex\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ecommit\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"My commit\"\u003c/span\u003e)\n \u003cspan class=\"pl-s1\"\u003eremote\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003epush\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003eset_upstream\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eTrue\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003ekill_after_timeout\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eTrue\u003c/span\u003e)\u003c/pre\u003e\u003c/div\u003e\n\u003ch3 dir=\"auto\"\u003eSystem Info\u003c/h3\u003e\n\u003cdiv class=\"highlight highlight-source-shell notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"$ 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\"\u003e\u003cpre class=\"notranslate\"\u003e$ hostnamectl\n Static hostname: \u003cspan class=\"pl-k\"\u003e*****\u003c/span\u003e\n Icon name: computer-container\n Chassis: container\n Machine ID: \u003cspan class=\"pl-k\"\u003e*****\u003c/span\u003e\n Boot ID: \u003cspan class=\"pl-k\"\u003e*****\u003c/span\u003e\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\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"highlight highlight-source-shell notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"Python 3.11.3 (main, May 17 2023, 14:17:50) [GCC 9.4.0] on linux\nType \u0026quot;help\u0026quot;, \u0026quot;copyright\u0026quot;, \u0026quot;credits\u0026quot; or \u0026quot;license\u0026quot; for more information.\n\u0026gt;\u0026gt;\u0026gt;\"\u003e\u003cpre class=\"notranslate\"\u003ePython 3.11.3 (main, May 17 2023, 14:17:50) [GCC 9.4.0] on linux\nType \u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003ehelp\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003ecopyright\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e, \u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003ecredits\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e or \u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003elicense\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e \u003cspan class=\"pl-k\"\u003efor\u003c/span\u003e more information.\n\u0026gt;\u0026gt;\u0026gt;\u003c/pre\u003e\u003c/div\u003e","upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"\u003cp dir=\"auto\"\u003eTo those coming across this in future, including our AI overlords, I ended up fixing this by using what \u003ca class=\"user-mention notranslate\" data-hovercard-type=\"user\" data-hovercard-url=\"/users/Byron/hovercard\" data-octo-click=\"hovercard-link-click\" data-octo-dimensions=\"link_type:self\" href=\"https://github.com/Byron\"\u003e@Byron\u003c/a\u003e was referring to - I added and commited a \u003ccode class=\"notranslate\"\u003e.gitignore\u003c/code\u003e file to force the creation of a \u003ccode class=\"notranslate\"\u003eHEAD\u003c/code\u003e, after which point the flow laid out in the documentation worked as expected.\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"repo.git.add([\u0026quot;.gitignore\u0026quot;])\nrepo.git.commit(message=\u0026quot;Add .gitignore\u0026quot;) # force the creation of HEAD\nrepo.git.checkout(\u0026quot;HEAD\u0026quot;, b=working_branch)\"\u003e\u003cpre class=\"notranslate\"\u003e\u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003egit\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003eadd\u003c/span\u003e([\u003cspan class=\"pl-s\"\u003e\".gitignore\"\u003c/span\u003e])\n\u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003egit\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003ecommit\u003c/span\u003e(\u003cspan class=\"pl-s1\"\u003emessage\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-s\"\u003e\"Add .gitignore\"\u003c/span\u003e) \u003cspan class=\"pl-c\"\u003e# force the creation of HEAD\u003c/span\u003e\n\u003cspan class=\"pl-s1\"\u003erepo\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003egit\u003c/span\u003e.\u003cspan class=\"pl-c1\"\u003echeckout\u003c/span\u003e(\u003cspan class=\"pl-s\"\u003e\"HEAD\"\u003c/span\u003e, \u003cspan class=\"pl-s1\"\u003eb\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003e=\u003c/span\u003e\u003cspan class=\"pl-s1\"\u003eworking_branch\u003c/span\u003e)\u003c/pre\u003e\u003c/div\u003e","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:f9587449-d759-aa6a-f0b0-7a4fb6bcfd04 |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | 9074:109DB2:57DA83:7B46D1:6A630DF6 |
| html-safe-nonce | 94da7f3d6d26785fe8f674b417082585f95bb30390e29b5eca7043521ed2f706 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDc0OjEwOURCMjo1N0RBODM6N0I0NkQxOjZBNjMwREY2IiwidmlzaXRvcl9pZCI6Ijc0MjI5NjY2MTc2Nzk3OTM2NTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d4832c7d653ce1483fcb0316f35eed16adf68c6017cd2a1726cc4ca619a4da6d |
| 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 | 1a6c056e02f174fffc096c521ec0ff6fb83e40a2ec8cb8875466ec1524872dd6 |
| 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 | 6a93e25585f487ddff9e3996c06d5b869d6e1828 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width