-
-
Notifications
You must be signed in to change notification settings - Fork 954
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
So what should I do? Stick to my workaround?
Beta Was this translation helpful? Give feedback.
All reactions
-
Either that or see if the git
binary can be upgraded to a more recent version. Only if its possible to execute git init --initial-branch=main
by hand on the system the software is deployed to will GitPython work as expected.
Beta Was this translation helpful? Give feedback.
All reactions
-
Either that or see if the
git
binary can be upgraded to a more recent version. Only if its possible to executegit init --initial-branch=main
by hand on the system the software is deployed to will GitPython work as expected.
I see
Does GitPython handle the flag init.defaultBranch?
Beta Was this translation helpful? Give feedback.
All reactions
-
It does not. In case it's unclear, it only calls the git
executable for most tasks, and init
is one of them. That's why this conversation isn't about GitPython, apparently, but really about the capabilities of the git
executable on the system the software gets deployed to.
Beta Was this translation helpful? Give feedback.
All reactions
-
Ok I get it now
Sorry it took me so long to go grok
Beta Was this translation helpful? Give feedback.