git 在 GitPython 中签出或列出远程分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2473035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checkout or list remote branches in GitPython
提问by Mike
I don't see an option to checkout or list remote/local branches in this module: https://gitpython.readthedocs.io/en/stable/
我在此模块中没有看到结帐或列出远程/本地分支的选项:https: //gitpython.readthedocs.io/en/stable/
采纳答案by Debilski
After you've done
完成后
from git import Git
g = Git()
(and possibly some other command to init g
to the repository you care about) all attribute requests on g
are more or less transformed into a call of git attr *args
.
(可能还有一些其他命令来初始化g
您关心的存储库)所有属性请求g
都或多或少地转换为git attr *args
.
Therefore:
所以:
g.checkout("mybranch")
should do what you want.
应该做你想做的。
g.branch()
will list the branches. However, note that these are very low level commands and they will return the exact code that the git executables will return. Therefore, don't expect a nice list. I'll just be a string of several lines and with one line having an asterisk as the first character.
将列出分支。但是,请注意,这些是非常低级的命令,它们将返回 git 可执行文件将返回的确切代码。因此,不要期望一个不错的列表。我只是一个由多行组成的字符串,其中一行的第一个字符是星号。
There might be some better way to do this in the library. In repo.py
for example is a special active_branch
command. You'll have to go through the source a little and look for yourself.
在图书馆中可能有一些更好的方法来做到这一点。在repo.py
例如是一个特殊的active_branch
命令。您必须稍微浏览一下源代码并寻找自己。
回答by pkowalczyk
To list branches currently you can use:
要列出当前的分支,您可以使用:
from git import Repo
r = Repo(your_repo_path)
repo_heads = r.heads # or it's alias: r.branches
r.heads
returns git.util.IterableList
(inherits after list
) of git.Head
objects, so you can:
r.heads
返回git.util.IterableList
(在 之后继承list
)git.Head
对象,因此您可以:
repo_heads_names = [h.name for h in repo_heads]
And to checkout eg. master
:
并结帐例如。master
:
repo_heads['master'].checkout()
# you can get elements of IterableList through it_list['branch_name']
# or it_list.branch_name
Module mentioned in the question is GitPython
which movedfrom gitorious
to Github.
回答by Marios V
I had a similar issue. In my case I only wanted to list the remote branches that are tracked locally. This worked for me:
我有一个类似的问题。就我而言,我只想列出本地跟踪的远程分支。这对我有用:
import git
repo = git.Repo(repo_path)
branches = []
for r in repo.branches:
branches.append(r)
# check if a tracking branch exists
tb = t.tracking_branch()
if tb:
branches.append(tb)
In case all remote branches are needed, I would prefer running git directly:
如果需要所有远程分支,我更愿意直接运行 git:
def get_all_branches(path):
cmd = ['git', '-C', path, 'branch', '-a']
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
return out
回答by parsley72
Just to make it obvious - to get a list of remote branches from the current repo directory:
只是为了让它变得明显 - 从当前 repo 目录中获取远程分支列表:
import os, git
# Create repo for current directory
repo = git.Repo(os.getcwd())
# Run "git branch -r" and collect results into array
remote_branches = []
for ref in repo.git.branch('-r').split('\n'):
print ref
remote_branches.append(ref)
回答by Don Charlie
For those who want to just print the remote branches:
对于那些只想打印远程分支的人:
# Execute from the repository root directory
repo = git.Repo('.')
remote_refs = repo.remote().refs
for refs in remote_refs:
print(refs.name)
回答by Tianle Xia
Basically, with GitPython, if you know how to do it within command line, but not within the API, just use repo.git.action("your command without leading 'git' and 'action'"), example: git log --reverse => repo.git.log('--reverse')
基本上,对于 GitPython,如果您知道如何在命令行中而不是在 API 中执行此操作,只需使用 repo.git.action("your command withoutleading 'git' and 'action'"),例如:git log - -reverse => repo.git.log('--reverse')
in this case https://stackoverflow.com/a/47872315/12550269
在这种情况下https://stackoverflow.com/a/47872315/12550269
So I try this command:
所以我试试这个命令:
repo = git.Repo()
repo.git.checkout('-b', local_branch, remote_branch)
this command can make a new local branch name local_branch
(if already haved, rasie error) and set up to track remote branch remote_branch
这个命令可以创建一个新的本地分支名称local_branch
(如果已经有了,rasie 错误)并设置为跟踪远程分支remote_branch
it works very well!
它工作得很好!