Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

unable to set initial-branch #1371

Answered by Byron
simkimsia asked this question in Q&A
Discussion options

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?

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

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.

You must be logged in to vote
5 replies
Comment options

So what should I do? Stick to my workaround?

Comment options

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.

Comment options

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.

I see

Does GitPython handle the flag init.defaultBranch?

Comment options

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.

Comment options

Ok I get it now

Sorry it took me so long to go grok

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.

AltStyle によって変換されたページ (->オリジナル) /