使用GitPython,我可以使用以下內容創建一個新的存儲庫:from git.repo.base import RepoRepo.init('/tmp/some-repo/')該存儲庫是使用默認分支創建的master。如何修改這個默認分支?更新:正如下面的答案中所建議的,我嘗試使用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-名稱gt)。從錯誤來看,我認為 GitPython 的附加 kwargs 功能不包括前綴--。
1 回答

瀟瀟雨雨
TA貢獻1833條經驗 獲得超4個贊
根據文檔,init
采用與關鍵字參數相同的參數git init
。你確實必須-
變成_
.
from?git?import?Repo Repo.init('/tmp/some-repo/',?initial_branch='main')
更新
initial-branch
最近在 v2.28.0 中添加。您需要升級 Git 才能使用它。
如果不能,請使用 手動更改分支名稱branch.rename(new_name)
。不幸的是,直到第一次提交之后你才能執行此操作,因為還沒有分支真正存在。這是 Git 的限制,也是他們添加的原因initial-branch
以及init.defaultBranch
配置選項。
添加回答
舉報
0/150
提交
取消