With GitPython
, I can create a new repo with the following:
from git.repo.base import Repo
Repo.init('/tmp/some-repo/')
The repo is created with the default branch master
.
How can I modify this default branch?
Update: As suggested in the answers below, I have tried using Repo.init('/tmp/some-repo', initial_branch="main"
), however it renders this exception:
Traceback (most recent call last):
File "/app/checker/tests.py", line 280, in test_alternative_compare_branch
comp_repo_main = Repo.init(
File "/usr/local/lib/python3.9/site-packages/git/repo/base.py", line 937, in init
git.init(**kwargs)
File "/usr/local/lib/python3.9/site-packages/git/cmd.py", line 542, in <lambda>
return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/git/cmd.py", line 1005, in _call_process
return self.execute(call, **exec_kwargs)
File "/usr/local/lib/python3.9/site-packages/git/cmd.py", line 822, in execute
raise GitCommandError(command, status, stderr_value, stdout_value)
git.exc.GitCommandError: Cmd('git') failed due to: exit code(129)
cmdline: git init --initial-branch=main
stderr: 'error: unknown option `initial-branch=main'
In the git docs, it states that the command for setting initial branch is --initial-branch
(https://git-scm.com/docs/git-init/2.28.0#Documentation/git-init.txt---initial-branchltbranch-namegt).
Judging by the error, I think that the additional kwargs feature of GitPython is not including the --
prefix.
repo
? did you clone it, or is it an existing local directory? What's the purpose of this - do you want to skip cloning some objects or just want to find a way to checkout an existing branch using gitpython? – Territorializerepo
is a new repo I have initialised using. I made a typo in the code above... I've corrected it now. – Meredeth