小编ype*_*eng的帖子

如何使用Pygithub签出到新分支?

这是我学到的

g = Github("user", "pass")
repoName = "apiTest"
print "Get all repos:"
for repo in g.get_user().get_repos():
print "\t%s" % repo.name

print "<--------------------------------------------------->"

print "Get all branches in repo %s:" % repoName
for branch in g.get_user().get_repo(repoName).get_branches():
print "\t%s" % branch.name

print "<--------------------------------------------------->"

print "Get last commit message in repo %s:" % repoName
branch = g.get_user().get_repo(repoName).get_branch("dev")
lastCommit = branch._commit.value.commit
print "\t%s" % lastCommit._message.value
print "\t%s"

print "<--------------------------------------------------->"
fc = repo.update_file("/README.md", "testing PyGithub", "test commit", fc.sha)
print fc
Run Code Online (Sandbox Code Playgroud)

但我想知道如何结帐到新分支。我没有在网上找到任何示例。非常感谢你。

python github github-api pygithub

2
推荐指数
1
解决办法
1268
查看次数

如何通过PyGithub将分支合并到master

比如我有一个文件t.json,内容是:

{
  "a": "abcdefg"
}
Run Code Online (Sandbox Code Playgroud)

文件 t.json 被推送到 master 分支。然后我向文件添加一些内容,并签出到新分支,因此文件现在如下所示:

{
  "a": "abcdefg",
  "b": "mkjuujj"
}
Run Code Online (Sandbox Code Playgroud)

现在我可以使用 PyGithub 比较两个提交。代码是这样的:

WORKING_BRANCH = "my_new_branch"
new_branch_ref_str = "refs/heads/%s" % WORKING_BRANCH
branch_ref = None
all_ref = repo.get_git_refs()

for ref in all_ref:
    if ref.ref == new_branch_ref_str:
        branch_ref = ref
        break

if not branch_ref:
    # create branch from this commit
    b = repo.get_branch("master")
    branch_ref = repo.create_git_ref(ref=new_branch_ref_str,
                                 sha=b.commit.sha)

    last_head = repo.get_branch(WORKING_BRANCH)
    fc = repo.get_file_contents("/t.json", ref=WORKING_BRANCH)
    file = 't.json'
    commit_message = "create a new branch with changes"
    input_file …
Run Code Online (Sandbox Code Playgroud)

python git version-control github-api pygithub

1
推荐指数
1
解决办法
2193
查看次数

标签 统计

github-api ×2

pygithub ×2

python ×2

git ×1

github ×1

version-control ×1