亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為 GitPython 設置默認分支

如何為 GitPython 設置默認分支

慕村225694 2023-12-12 15:44:18
使用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配置選項。


查看完整回答
反對 回復 2023-12-12
  • 1 回答
  • 0 關注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號