6

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.

2
  • what is in 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? Commented Oct 21, 2020 at 19:08
  • Hi @Marat, the repo is a new repo I have initialised using. I made a typo in the code above... I've corrected it now. Commented Oct 22, 2020 at 18:31

1 Answer 1

9

According to the docs, init takes the same arguments as git init as keyword arguments. You do have to turn - into _.

from git import Repo

Repo.init('/tmp/some-repo/', initial_branch='main')

UPDATE

initial-branch was added very recently in v2.28.0. You'll need to upgrade Git to use it.

If you can't, manually change the branch name with branch.rename(new_name). Unfortunately you can't do this until after the first commit, no branches truly exist yet. That's a Git limitation and why they added initial-branch and also the init.defaultBranch config option.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes I did see that but when I try your example it throws an error: stderr: 'error: unknown option initial-branch=main'... I need the two -- prefix but I can't figure out how to set it.
@LondonAppDev It's initial_branch. Note the underscore. You do not need the --.
Hey @Schwern, yes I tried that, but it didn't work. I edited the post with the details. Thanks for responding though!
@LondonAppDev That shows it's running git init --initial-branch=main which is correct. Check what version of Git GitPython is using. from git import Git, g = Git() and g.version_info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.