HEAD does not resolve when trying to checkout a branch (Python 3.11 on WSL2 - Ubuntu 20.04) #1928
-
I've created a new git repository by calling The 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. I 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 My code is as follows: def create_repo(
working_branch: str,
repo_name: str,
repo_url: str,
)
# repo_name = myorganisation/my-repo
# repo_url turns repo_name into a GitHub SSH URL
# working_branch is what it sounds like. Note that the default branch for my organisation is named "main"
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
subprocess.run(args=["git", "init"])
repo = git.Repo(str(repo_path))
remote_name = "origin"
remote = repo.create_remote(remote_name, repo_url)
# maybe the remote doesn't exist?
remote.fetch() # attempt to fetch from the remote in case that's needed to update refs
print(os.listdir()) # indeed, there are files present besides the .git folder
repo.head.reference = repo.create_head(working_branch) # raises the error mentioned
remote_ref = git.RemoteReference(
repo=repo,
path=f"refs/remotes/{remote_name}/{working_branch}",
)
repo.head.reference.set_tracking_branch(remote_ref)
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
branch_obj.checkout()
repo.index.add(["."])
repo.index.commit("My commit")
remote.push(set_upstream=True, kill_after_timeout=True) System Info$ hostnamectl
Static hostname: *****
Icon name: computer-container
Chassis: container
Machine ID: *****
Boot ID: *****
Virtualization: wsl
Operating System: Ubuntu 20.04.5 LTS
Kernel: Linux 5.15.146.1-microsoft-standard-WSL2
Architecture: x86-64 Python 3.11.3 (main, May 17 2023, 14:17:50) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
That seems like very complicated way to create a clone of a private repository. Indeed, there will be an exception when a method is run that tries to dereference a Either that commit would have to be created or the situation is avoided by cloning right away. It's interesting that never in my life I have initialized a repository, created a remote, and fetched the remote, without having a valid (i.e. existing) |
Beta Was this translation helpful? Give feedback.
-
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 repo.git.add([".gitignore"])
repo.git.commit(message="Add .gitignore") # force the creation of HEAD
repo.git.checkout("HEAD", b=working_branch) |
Beta Was this translation helpful? Give feedback.
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 aHEAD
, after which point the flow laid out in the documentation worked as expected.