-
-
Notifications
You must be signed in to change notification settings - Fork 954
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 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
.
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 repo.index.add(["."])
then tried creating the HEAD again, still no luck.
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.
All reactions
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.
repo.git.add([".gitignore"]) repo.git.commit(message="Add .gitignore") # force the creation of HEAD repo.git.checkout("HEAD", b=working_branch)
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 HEAD
in a newly initialized repository, one that has no commit.
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) HEAD
reference. Thus it could be that this workflow isn't well supported, code might run into a chicken-egg problem, where you can't checkout a branch while HEAD
is unborn (i.e. in a newly initialized state).
Beta Was this translation helpful? Give feedback.
All reactions
-
The fetch was intended as an experiment more than anything in case GitHub created any refs during the creation process.
Beta Was this translation helpful? Give feedback.
All reactions
-
Your comments led me to try committing a file and then proceeding to call repo.create_head(...)
which worked well, thank you!
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
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.
All reactions
-
Thanks for sharing these instructions - I think they are a valid workaround to something that can be expected to work better.
Beta Was this translation helpful? Give feedback.