Skip to content
\n

gives me

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

Workaround

\n

I have to resort to this

\n
init_git(path_to_folder_with_no_working_tree)\n\ndef execute(*args, supress_exception=False, cwd=None):\n    cur_dir = os.getcwd()\n\n    try:\n        if cwd:\n            os.chdir(cwd)\n\n        proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n\n        out, err = proc.communicate()\n        out = out.decode(\"utf-8\")\n        err = err.decode(\"utf-8\")\n        if err and not supress_exception:\n            raise Exception(err)\n        else:\n            return out\n    finally:\n        os.chdir(cur_dir)\n\n\ndef init_git(project_directory):\n    # workaround for issue #1\n    if not os.path.exists(os.path.join(project_directory, \".git\")):\n        execute(\n            \"git\",\n            \"config\",\n            \"--global\",\n            \"init.defaultBranch\",\n            \"main\",\n            cwd=project_directory,\n        )\n        execute(\"git\", \"init\", cwd=project_directory)\n
\n

Is there a better way to do the same thing?

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

GitPython does nothing more but to execute git like so: git init --initial-branch=main, which might not work with all git versions. My suspicion is that the flag is a more recent feature than the config option, which is why that one works. On 2.30.1 it's definitely available, and it seems to have been added about a year ago.

","upvoteCount":3,"url":"https://github.com/gitpython-developers/GitPython/discussions/1371#discussioncomment-1538399"}}}

unable to set initial-branch #1371

Answered by Byron
simkimsia asked this question in Q&A
Oct 25, 2021 · 1 comments · 5 replies
Discussion options

You must be logged in to vote

GitPython does nothing more but to execute git like so: git init --initial-branch=main, which might not work with all git versions. My suspicion is that the flag is a more recent feature than the config option, which is why that one works. On 2.30.1 it's definitely available, and it seems to have been added about a year ago.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@simkimsia
Comment options

@Byron
Comment options

@simkimsia
Comment options

@Byron
Comment options

@simkimsia
Comment options

Answer selected by simkimsia
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1370 on October 26, 2021 12:48.