Skip to content

unable to set initial-branch #1370

Description

@simkimsia
git_repo = git.Repo.init(path_to_folder_with_no_working_tree, initial_branch="main")

gives me

git.exc.GitCommandError: Cmd('git') failed due to: exit code(129)
  cmdline: git init --initial-branch=main
  stderr: 'error: unknown option `initial-branch=main'
usage: git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]

Workaround

I have to resort to this

init_git(path_to_folder_with_no_working_tree)

def execute(*args, supress_exception=False, cwd=None):
    cur_dir = os.getcwd()

    try:
        if cwd:
            os.chdir(cwd)

        proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        out, err = proc.communicate()
        out = out.decode("utf-8")
        err = err.decode("utf-8")
        if err and not supress_exception:
            raise Exception(err)
        else:
            return out
    finally:
        os.chdir(cur_dir)


def init_git(project_directory):
    # workaround for issue #1
    if not os.path.exists(os.path.join(project_directory, ".git")):
        execute(
            "git",
            "config",
            "--global",
            "init.defaultBranch",
            "main",
            cwd=project_directory,
        )
        execute("git", "init", cwd=project_directory)

Is there a better way to do the same thing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions