如何为 GitPython 设置默认分支
How to set default branch for GitPython
使用 GitPython
,我可以使用以下内容创建一个新的存储库:
from git.repo.base import Repo
Repo.init('/tmp/some-repo/')
回购是使用默认分支 master
创建的。
如何修改这个默认分支?
Update:正如下面答案中所建议的,我尝试使用 Repo.init('/tmp/some-repo', initial_branch="main"
),但是它呈现了这个异常:
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'
在git文档中,它指出设置初始分支的命令是--initial-branch
(https://git-scm.com/docs/git-init/2.28.0#Documentation/git-init.txt---initial-branchltbranch-namegt)。
从错误判断,我认为 GitPython 的附加 kwargs 特性不包括 --
前缀。
According to the docs、init
采用与 git init
相同的参数作为关键字参数。你必须把 -
变成 _
.
from git import Repo
Repo.init('/tmp/some-repo/', initial_branch='main')
更新
initial-branch
was added very recently 在 v2.28.0 中。您需要升级 Git 才能使用它。
如果不能,请使用 branch.rename(new_name)
手动更改分支名称。不幸的是,在第一次提交 之后,你不能这样做,还没有分支真正存在。这是一个 Git 限制,也是他们添加 initial-branch
和 init.defaultBranch
config option.
的原因
使用 GitPython
,我可以使用以下内容创建一个新的存储库:
from git.repo.base import Repo
Repo.init('/tmp/some-repo/')
回购是使用默认分支 master
创建的。
如何修改这个默认分支?
Update:正如下面答案中所建议的,我尝试使用 Repo.init('/tmp/some-repo', initial_branch="main"
),但是它呈现了这个异常:
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'
在git文档中,它指出设置初始分支的命令是--initial-branch
(https://git-scm.com/docs/git-init/2.28.0#Documentation/git-init.txt---initial-branchltbranch-namegt)。
从错误判断,我认为 GitPython 的附加 kwargs 特性不包括 --
前缀。
According to the docs、init
采用与 git init
相同的参数作为关键字参数。你必须把 -
变成 _
.
from git import Repo
Repo.init('/tmp/some-repo/', initial_branch='main')
更新
initial-branch
was added very recently 在 v2.28.0 中。您需要升级 Git 才能使用它。
如果不能,请使用 branch.rename(new_name)
手动更改分支名称。不幸的是,在第一次提交 之后,你不能这样做,还没有分支真正存在。这是一个 Git 限制,也是他们添加 initial-branch
和 init.defaultBranch
config option.