如何检查出有GitPython分支

问题描述 投票:7回答:1

我已经克隆与GitPython的仓库,现在我想签一个分支,并与该分支的内容更新本地资源库的工作树。理想情况下,我还可以检查分支做这些之前存在。这是我到目前为止有:

import git

repo_clone_url = "[email protected]:mygithubuser/myrepo.git"
local_repo = "mytestproject"
test_branch = "test-branch"
repo = git.Repo.clone_from(repo_clone_url, local_repo)
# Check out branch test_branch somehow
# write to file in working directory
repo.index.add(["test.txt"])
commit = repo.index.commit("Commit test")

我不知道要放什么东西在上面的评论的地方。该documentation似乎给出了如何拆卸头,但不知道如何检出一个名为分支的例子。

python git gitpython
1个回答
14
投票

如果分支存在:

repo.git.checkout('branchename')

如果不:

repo.git.checkout('-b', 'branchename')

基本上,GitPython,如果你知道如何命令行内做到这一点,但不是API中,只要使用repo.git.action("your command without leading 'git' and 'action'"),例如:git log --reverse => repo.git.log('--reverse')

© www.soinside.com 2019 - 2024. All rights reserved.