[英]How to clone from specific branch from Git using Gitpython
I tried to clone a repository from git using GitPython in python function.我试图在 python 函数中使用 GitPython 从 git 克隆一个存储库。 I used GitPython library for cloning from git in my python function and my code snippet as follows:
我在我的 python 函数和我的代码片段中使用 GitPython 库从 git 克隆,如下所示:
from git import Repo
从 git 导入回购
Repo.clone_from(' http://user:password@github.com/user/project.git ', /home/antro/Project/')
Repo.clone_from(' http://user:password@github.com/user/project.git ', /home/antro/Project/')
It clones from master branch.它从主分支克隆。 How do I clone from other branch using GitPython or any other library is available to clone from individual branches?
如何使用 GitPython 从其他分支克隆或任何其他库可用于从单个分支克隆? Please let me know.
请告诉我。
I am aware of clone by mentioning branch in commandline using我通过在命令行中提及分支来了解克隆
git clone -b branch http://github.com/user/project.git git clone -b 分支http://github.com/user/project.git
just pass the branch name parameter, eg :-只需传递分支名称参数,例如:-
repo = Repo.clone_from(
'http://user:password@github.com/user/project.git',
'/home/antro/Project/',
branch='master'
)
From toanant's answer.来自toanant的回答。
This works for me with the --single-branch
option这对我
--single-branch
选项
repo = Repo.clone_from(
'http://user:password@github.com/user/project.git --single-branch',
'/home/antro/Project/',
branch='master'
)
对于--single-branch选项,您只需将single_branch
参数传递给Repo.clone_from()
方法:
Repo.clone_from(repo, path, single_branch=True, b='branch')
GitPython uses a keyword args transformation under the hood: GitPython 在底层使用关键字 args 转换:
# cmd.py
def transform_kwarg(self, name: str, value: Any, split_single_char_options: bool) -> List[str]:
if len(name) == 1:
if value is True:
return ["-%s" % name]
elif value not in (False, None):
if split_single_char_options:
return ["-%s" % name, "%s" % value]
else:
return ["-%s%s" % (name, value)]
else:
if value is True:
return ["--%s" % dashify(name)]
elif value is not False and value is not None:
return ["--%s=%s" % (dashify(name), value)]
return []
A resulting list of command parts is fed into subprocess.Popen
, so you do not want to add --single-branch
to the repo URL.所产生的命令零件清单被送入
subprocess.Popen
,所以你不希望添加--single-branch
到回购网址。 If you do, a strange list will be passed to Popen.如果你这样做,一个奇怪的列表将传递给 Popen。 For example:
例如:
['-v', '--branch=my-branch', 'https://github.com/me/my-project.git --single-branch', '/tmp/clone/here']
However, armed with this new information, you can pass any git
CLI flags you like just by using the kwargs
.但是,有了这些新信息,您可以仅使用
kwargs
传递您喜欢的任何git
CLI 标志。 You may then ask yourself, "How do I pass in a dash to a keyword like single-branch
?"然后你可能会问自己,“我如何将破折号传递给像
single-branch
这样的关键字?” That's a no-go in Python.这在 Python 中是行不通的。 You will see a
dashify
function in the above code which transforms any flag from, say, single_branch=True
to single-branch
, and then to --single-branch
.您将在上面的代码中看到一个
dashify
函数,它将任何标志从例如single_branch=True
为single-branch
,然后转换为--single-branch
。
Here is a useful example for cloning a single, shallow branch using a personal access token from GitHub:这是使用来自 GitHub 的个人访问令牌克隆单个浅分支的有用示例:
repo_url = "https://github.com/me/private-project.git"
branch = "wip-branch"
# Notice the trailing : below
credentials = base64.b64encode(f"{GHE_TOKEN}:".encode("latin-1")).decode("latin-1")
Repo.clone_from(
url=repo_url,
c=f"http.{repo_url}/.extraheader=AUTHORIZATION: basic {credentials}",
single_branch=True,
depth=1,
to_path=f"/clone/to/here",
branch=branch,
)
The command list sent to Popen
then looks like this:发送到
Popen
的命令列表如下所示:
['git', 'clone', '-v', '-c', 'http.https://github.com/me/private-project.git/.extraheader=AUTHORIZATION: basic XTE...UwNTo=', '--single-branch', '--depth=1', '--bare', '--branch=wip-branch', 'https://github.com/me/private-project.git', '/clone/to/here']
(PSA: Please do not actually send your personal tokens as part of the URL before the @
.) (PSA:请不要实际上将您的个人令牌作为
@
之前的 URL 的一部分发送。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.